Working with Different Time Zones in SQL Server 2008

SQL Server 2008 introduces the new DATETIMEOFFSET data type that provides a time zone offset for working with dates and times in different time zones.

Note: SQL Server 2008 adds four new date and time data types: date, time, datetime2, and datetimeoffset.

The current date and time (with the offset) can be obtained using SYSDATETIMEOFFSET.To convert values from one timezone to another, you can use the SWITCHOFFSET function. Let us see this in action while converting your local timezone to Pacific Standard Time (US West Coast; Los Angeles, CA)

DECLARE @local DATETIMEOFFSET = SYSDATETIMEOFFSET()
SELECT SYSDATETIMEOFFSET() as 'CurrentDT-W/Offset'
SELECT SWITCHOFFSET (@local, '-08:00') as 'ConvertedDT-W/Offset'

OUTPUT

image

Note: When you are working with a DateTimeOffset, you can specify an offset that complies with the ISO 8601 standard.


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

No comments: