SQL Server - Convert date field to UTC

前端 未结 12 739
故里飘歌
故里飘歌 2020-12-23 13:39

I have recently updated my system to record date/times as UTC as previously they were storing as local time.

I now need to convert all the local stored date/times to

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 13:53

    If they're all local to you, then here's the offset:

    SELECT GETDATE() AS CurrentTime, GETUTCDATE() AS UTCTime
    

    and you should be able to update all the data using:

    UPDATE SomeTable
       SET DateTimeStamp = DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), DateTimeStamp)
    

    Would that work, or am I missing another angle of this problem?

提交回复
热议问题