sql-serverdatetimeutc

SQL Server - Convert date field to UTC


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 UTC. I was wondering if there is any built in function, similar to .NET's ConvertTime method?

I am trying to avoid having to write a utility app to do this for me.

Any suggestions?


Solution

  • 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?