我正在尝试将临时变量转换为UTC
时间并使用以下内容打印它,
declare @dt datetime, @dtEnd datetime
set @dt = '2017-04-25' AT TIME ZONE 'UTC'
set @dtEnd = '2017-04-30' AT TIME ZONE 'UTC'
select dateadd(day, number, @dt)
from
(select distinct number from master.dbo.spt_values
where name is null
) n
where dateadd(day, number, @dt) < @dtEnd
我从接受的答案here获得了此代码。 我收到了一个错误,
参数数据类型varchar对于AT TIME ZONE的参数1无效 功能
我正在使用SQL Server docs将CDT
转换为UTC
。链接的答案很适合在没有AT TIME ZONE 'UTC'
的情况下打印。
如何在UTC
时间内将临时变量转换为打印?
答案 0 :(得分:1)
您需要使用强制转换作为日期时间
declare @dt datetime, @dtEnd datetime
set @dt = CAST('2017-04-25' AS DATETIME) AT TIME ZONE 'Central Standard Time' AT TIME ZONE 'UTC'
set @dtEnd = CAST('2017-04-30' AS DATETIME) AT TIME ZONE 'Central Standard Time' AT TIME ZONE 'UTC'
select dateadd(day, number, @dt)
from
(select distinct number from master.dbo.spt_values
where name is null
) n
where dateadd(day, number, @dt) < @dtEnd