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)

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.


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


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

 
  Feedback:

comments

0 Responses to "Working with Different Time Zones in SQL Server 2008"
 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions