Search code examples
sql-server-2012calculated-columnsisodate

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


convert(datetime,'2015-03-06T23:59:04Z',127) yields a MS-SQL datetime:

2015-03-06 23:59:04.000

If we have an actual column called [isodate] defined either as varchar(20) or char(20), is it possible to use that conversion as the formula of a Computed Column Specification in SQL Server 2012?

I am getting "error validating the formula" with eithe of these formulas:

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


Solution

  • It looks like you just have a syntax error. Removing the brackets from [datetime] in your formula should do the trick.

    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)