T-SQL 101: #77 Switching timezone offsets in SQL Server T-SQL with SWITCHOFFSET

I mentioned in the last T-SQL post how you could create a datetimeoffset value by combining a datetime2 with an offset. But sometimes you need to switch to a different offset. And that's what the SWITCHOFFSET() function does:

In this query, I was taking a local time (that had a timezone offset of 11 hours), and switching it to the current time in Seattle (with a -7 hours offset):

So this function allows us to take a datetimeoffset value and convert it to a different offset.

Keep in mind that even though we have handling of timezone offsets, SQL Server isn't timezone aware to any great deal. However, there is a function sys.time_zone_info that will tell you the current offsets and daylight savings settings for different timezones.

What it doesn't do is provide you with future or historical timezone info.

Learning T-SQL

It's worth your while becoming proficient in SQL. If you'd like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.

Leave a Reply

Your email address will not be published.