简体   繁体   中英

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 ?

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.

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM