August 10, 2010

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)

SELECT SWITCHOFFSET (@local, '-08:00') as 'ConvertedDT-W/Offset'



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, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

No comments: