cnd
cnd

Reputation: 33794

Convert DateTime with milliseconds to SQL DateTime

I'm making a query like this :

@" if (" + logic + @") 
begin
    INSERT INTO [FRIIB].[dbo].[Incidents]
        ([RecTime]
        ,[Name]
        ,[Message])
    VALUES
        ('"     + dt.ToString("yyyy/MM/dd hh:mm:ss.fff") + //ODBC : Canonical DT Format

and got SQL like this :

if (( 1 = 1 )) 
begin
INSERT INTO [FRIIB].[dbo].[Incidents]
([RecTime],[Name],[Message])
VALUES 
('2011.02.14 04:30:10.020',

and error like this :

can't convert varchar to datetime

How can I fix it ? What am I doing wrong ?

Upvotes: 0

Views: 1003

Answers (2)

AbhiRoczz...
AbhiRoczz...

Reputation: 252

Hey you do something like this..

  java.util.Date dt = new java.util.Date();

  java.sql.Date sqlDate = new java.sql.Date(dt.getDate());

and pass this sqlDate as a paremeter..

Upvotes: 0

Simon Mourier
Simon Mourier

Reputation: 139316

SQL Documentation about this is here: Supported String Literal Formats for datetime

So you could use ISO8601 (example: 2004-05-23T14:25:10.487), because, as stated in the doc:

The advantage in using the ISO 8601 format is that it is an international standard with unambiguous specification. Also, this format is not affected by the SET DATEFORMAT or SET LANGUAGE setting.

Upvotes: 1

Related Questions