每MSDN convert 应该使用 127 作为样式参数正确解析带有时区的 ISO 8601 日期。
The optional time zone indicator, Z, is used to make it easier to map XML datetime values that have time zone information to SQL Server datetime values that have no time zone. Z is the indicator for time zone UTC-0. Other time zones are indicated with HH:MM offset in the + or - direction. For example: 2006-12-12T23:45:12-08:00.
以下所有内容都是有效的 ISO 8601 日期,但返回从字符串转换日期和/或时间时转换失败。
select convert(datetime, N'2014-02-07T13:51:00+07:00', 127)
select convert(datetime, N'2014-02-07T13:51:00+07', 127)
select convert(datetime, N'2006-12-12T23:45:12-08:00', 127)
有人对这个问题有解决方案或解决方法吗?
最佳答案
解决方法?:使用 datetimeoffset
:
select convert(datetimeoffset, N'2014-02-07T13:51:00+07:00', 127) --<-- This one works...
select convert(datetimeoffset, N'2014-02-07T13:51:00+07', 127)
select convert(datetimeoffset, N'2006-12-12T23:45:12-08:00') --<-- and this one works...
关于SQL Server 转换 ISO 8601 未按记录工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21621650/