如何将 sql 偏移日期时间格式转换回默认格式?

How to convert sql offsetted datetime format back to default?

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

,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

据我所知,您将 "local" 时间转换为 UTC 日期时间并且您想恢复为当地时间?这演示了一种方法。

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

要将存储为 UTC 日期时间的日期转换为本地时间,我认为您已经非常接近了

select SWITCHOFFSET( CAST(YourStoredDateTime AS datetimeoffset),DATEDIFF(minute, GETUTCDATE(), GETDATE())) AS CREATEDDATETIMEPDT</pre>

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