SQL Server AT TIME ZONE error
Contents
Problem
Being that I work for a global company, I decided we should store our datetimes using the datetimeoffset
data type in SQL Server and that we could report in the local timezone or whatever timezone is desired. I send the timezone to a stored procedure as a parameter. Inside the stored procedure I use AT TIME ZONE
to convert dates to the desired timezone. I was using TimeZoneInfo.Local.StandardName
as the value I sent which worked great in Eastern Standard Time and many other timezones for that matter. However, I ran into trouble when we had a new machine in the Malay Penisula Standard Time. The reports suddenly started throwing an error.
Solution
After inspecting some timezone info using PowerShell and testing the proposed fix in SQL Server, I realized I needed to use the Id rather than the StandardName.