Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to ConvertTimeFromUtc in MS SQL Server?

Is there a function in SQL Server that will convert a DateTime saved as UTC time to the local time? The problem is that I have times saved as UTC times, and I used to calculate time zone offset as

select @tzoffset = datediff(mi,SYSDATETIMEOFFSET(),SYSDATETIME())

and adding that to my UTC times, which worked fine until daylight saving came. As that selects the current time zone offset, the calculation is invalid for pre-DST values.

As the dates are rendered in ASP.NET web form, I have worked around the issue by rendering the dates as TimeZoneInfo.ConvertTimeFromUtc((DateTime)Eval("maxtime"), TimeZoneInfo.Local), which works automagically.

Is there something similarly elegant in T-SQL?

like image 435
Axarydax Avatar asked Jan 31 '26 22:01

Axarydax


1 Answers

It is not that simple. Read this http://blogs.msdn.com/b/bartd/archive/2009/03/31/the-death-of-datetime.aspx

like image 185
IvanP Avatar answered Feb 03 '26 18:02

IvanP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!