【问题标题】:Convert DateTime with milliseconds to SQL DateTime以毫秒为单位的 DateTime 转换为 SQL DateTime
【发布时间】:2011-04-25 06:48:59
【问题描述】:

我正在做这样的查询:

@" 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

得到这样的 SQL:

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

和这样的错误:

无法将 varchar 转换为日期时间

我该如何解决?我做错了什么?

【问题讨论】:

    标签: .net sql datetime


    【解决方案1】:

    关于此的 SQL 文档在这里:Supported String Literal Formats for datetime

    因此您可以使用 ISO8601(例如:2004-05-23T14:25:10.487),因为如文档中所述:

    使用 ISO 8601 的优势 格式是它是国际 标准明确 规格。此外,这种格式是 不受 SET DATEFORMAT 或 设置语言设置。

    【讨论】:

    • 如何将 DateTime 转换为 2004-05-23T14:25:10.487 ?
    【解决方案2】:

    嘿,你做这样的事情..

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

    并将这个 sqlDate 作为参数传递..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2015-09-16
      • 2011-08-22
      • 2012-09-13
      • 1970-01-01
      相关资源
      最近更新 更多