【问题标题】:How to convert string to dateTime2(7) T-SQL如何将字符串转换为 dateTime2(7) T-SQL
【发布时间】:2019-04-16 12:31:17
【问题描述】:

我正在尝试将createdDatetime 插入到表格中。

我收到此错误:

从字符串转换日期和/或时间时转换失败

这就是我想要做的。

DECLARE @CreatedDatetime DATETIME2(7) = CONVERT(DATETIME2(7), '2018-11-13 10:57:13.5235756')

INSERT INTO [dbo].[Table](CreatedDatetime) 
VALUES (@CreatedDatetime)

SELECT Updated = @@ROWCOUNT

【问题讨论】:

    标签: tsql sql-server-2016 sql-server-2017


    【解决方案1】:

    添加121 表示ODBC canonical。见CAST and CONVERT (Transact-SQL)

    Declare @CreatedDatetime datetime2(7) = CONVERT(datetime2(7), '2018-11-13 10:57:13.5235756', 121)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      相关资源
      最近更新 更多