如何将sql offsetted datetime格式转换回默认值?

时间:2015-01-09 18:34:14

标签: sql sql-server

我正在抵消时区差异的列。如何将返回的日期时间转换回默认值,因为它现在在列中显示偏移信息?

,SWITCHOFFSET(CAST(CREATEDDATETIME AS datetimeoffset)  
,DATEDIFF(minute, GETUTCDATE(), GETDATE())) AS CREATEDDATETIMEPDT

2014-07-01 16:44:15.000 2014-07-01 08:44:15.0000000 -08:00

1 个答案:

答案 0 :(得分:1)

如果我了解您将“本地”时间转换为UTC日期时间并且您想要回到当地时间?这证明了一种方式。

select Dateadd(minute,datepart(TZoffset, CREATEDDATETIMEPDT), convert(datetime,CREATEDDATETIMEPDT)) as LocalTime
from
    (select 
        SWITCHOFFSET(   CAST(Getdate() AS datetimeoffset)
,DATEDIFF(minute, GETUTCDATE(), GETDATE()) ) AS CREATEDDATETIMEPDT ) UTC

要从存储为UTC日期时间的日期到当地时间,我认为你非常接近

select SWITCHOFFSET( CAST(YourStoredDateTime AS datetimeoffset),DATEDIFF(minute, GETUTCDATE(), GETDATE())) AS CREATEDDATETIMEPDT

然后像我上面那样使用dateadd()。