solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page

SQL Product Highlight

Idera - SQL safe backup

Idera's SQL safe provides a high-performance backup and recovery solution for Microsoft SQL Server. SQL safe saves money by reducing database backup time by up to 50% over native backups and reducing backup disk space requirements by up to 95%. SQL safe also enables complete "hands-free" automated backup of your entire SQL Server infrastructure and ensures compliance with your organization's backup and recovery policies. From implementations with tens of SQL servers to enterprises with hundreds of servers spread around the globe, SQL safe is the only SQL Server backup and recovery solution that scales to meet the challenge.

Learn more!








Convert SQL Server DateTime Data Type to DateTimeOffset Data Type

By: | Read Comments (3) | Print

John is a DBA and Software Developer with expertise in data modeling, database design, administration and development.

Related Tips: More

Problem

I saw this tip on new date types provided with SQL Server 2008. I am interested in using the datetimeoffset because we support end users around the world. How do I convert the datetime data types to the datetimeoffset data type in SQL Server 2008?

Solution

Converting data in a datetime column to datetimeoffset is straight forward, but it will probably give you an unexpected conversion. By default the time zone offset is zero. Unless all your data resides in Iceland or the other countries with a time zone of UTC-0 this is going to be a problem.

The following example shows you how to convert your time through a set of scripts.

Note: DateTimeOffset is a new feature in SQL Server 2008. If your instance of SQL Server is before SQL Server 2008 this code will not work.


Step 0 - Create an example table and insert sample date time values as shown in the script below.

CREATE TABLE dbo.DateTimeDemo
(DateTimeExample DATETIME, -- current date type
 DateTimeOffsetExample DATETIMEOFFSET) -- new date type
-- Insert current date to convert to datetimeoffset
INSERT INTO dbo.DateTimeDemo (DateTimeExample)
VALUES (GETDATE())
GO

Step 1 - We will convert the DateTimeExample column to DateTimeOffset data type and store it in the DateTimeOffsetExample column. You will notice that the offset is zero. This is the default. Unless your data resides in Iceland or another country with a time zone of UTC-0 this is going to be a problem. We will show you this step because we want to make sure you don't do this in your conversion. We will correct the time zone a little later in the demo.

UPDATE dbo.DateTimeDemo
SET DateTimeOffsetExample = DateTimeExample -- time zone = 00:00
GO
-- Let's look at our conversion. You should see (+00:00) time zone 
SELECT DateTimeExample, DateTimeOffsetExample
FROM dbo.DateTimeDemo

You should see something similar to the screenshot provided below.

new date types provided with sql server 2008

Step 2 - For this example we will assume that the data resides on the east coast. We will convert the data to East Standard Time. This is done by converting the data to a varchar and appending the time zone value.

UPDATE dbo.DateTimeDemo
SET DateTimeOffsetExample = 
          CONVERT(varchar(50), DateTimeExample, 120)+ ' -5:00'
SELECT DateTimeExample,
       DateTimeOffsetExample
FROM dbo.DateTimeDemo

You should see something similar to the screenshot provided below.

convert the datetime data types to the datetimeoffset data type in sql server 2008

Step 3 Now that the DateTimeOffset is stored with the correct time zone lets show the time to the user as local time. In this example we will assume that the application us running by a user in Central Standard Time. We will use the SWITCHOFFSET function to change the time.

SELECT DateTimeExample,
       DateTimeOffsetExample,
       SWITCHOFFSET (DateTimeOffsetExample , '-06:00')
       AS DateTimeSavingsOffsetExample
FROM dbo.DateTimeDemo

You should see something similar to the screenshot provided below. Notice that the time shown for the DateTimeSavingsOffsetExample column is in Central Standard Time (1 hour behind Eastern Standard Time) and the offset changed by one hour.

if you have users in different times zones you can manage the time zone specific date/times

Next Steps



Related Tips: More | Become a paid author


Last Update: 2/23/2011

Share: Share 






Comments and Feedback:

Wednesday, February 23, 2011 - 5:30:04 PM - h_d_t Read The Tip

what about daylight savings :(.... where timezones get a +1 or -1 during certain months of the year. and it does not always change on the same day each year, so you'll need a historical table of DST changes, and apply those when setting back to UTC..


Thursday, February 24, 2011 - 2:34:08 AM - chandra Read The Tip

In step 2 you are hardcoding EST with -5, which should be automatically calculated based on user's locale.


Sunday, February 27, 2011 - 9:16:41 AM - John Sterrett Read The Tip

Hi h_d_t,

If you are using C# or VB.NET there is code built in you can use to determine if the the time is daylight or standard time. I will crank out a tip soon to show this.

Regards,
John



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
Find and fix SQL Server problems before they happen - SQL diagnostic manager now with predictive analysis!

Write, edit, and explore SQL effortlessly with SQL Prompt.

SQL Servers keeping you up at night? Contact the Edgewood SQL Server consultants for a Health Check.

Find and Fix SQL issues with Foglight Performance Analysis. Get a free copy.

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Valuable SQL Server web casts on Performance Tuning, Development, Administration, SSIS and more...


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com