Thursday, May 3, 2018

sql server local time and utc time


there is a nice way to make a local DateTime value to a UTC DateTime value. First, I thought I can add a fixed amount of hours, but this is unfortunately not ok because change summer- to winter-time made the calculation invalid. So I found a very nice trick on StackOverflow... -> SQL Server

select getdate() as localtime, getutcdate() as utctime, datediff(hour, getdate(), getutcdate())

We can check the local time and the UTC time and add the delta as the offset to the local time. This is unfortunately only true if the local time value is in the same timezone as you are currently, but for my case it worked...


No comments: