计算列规范,用于将带时区 Z 的 ISO8601 日期转换为 SQL 日期时间

computed column specification to convert ISO8601 date with timezone Z to SQL datetime

convert(datetime,'2015-03-06T23:59:04Z',127) 产生一个 MS-SQL 日期时间:

2015-03-06 23:59:04.000

如果我们有一个名为 [isodate] 的实际列定义为 varchar(20) 或 char(20),是否可以将该转换用作 计算列规范 在 SQL Server 2012 中?

我用以下任一公式得到 "error validating the formula":

(convert([datetime],[isodate],127))
(convert(datetime,[isodate],127))

您似乎只是遇到了语法错误。从公式中的 [datetime] 中删除括号应该可以解决问题。

if object_id('tempdb..#temp') is not null drop table #temp

create table #temp (isodate varchar(20))

insert into #temp (isodate) values
('2015-03-06T23:59:04Z'),
('2016-03-04T13:59:04Z')

select isodate, convert(datetime,[isodate],127) as DT from #temp

--select convert(datetime,'2015-03-06T23:59:04Z',127)