[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert existing datetimes to UTC

Posted on 2012-09-09
12
Medium Priority
?
676 Views
Last Modified: 2013-01-07
Hi

I have a table (Sql Server 2008) with a timestamp column (getdate()) with a bunch of existing records.  I want to convert all of the existing timestamps to their UTC equivalents.

What's the easiest way to do this?

Many thanks!
0
Comment
Question by:Danzigger
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 38382116
Select convert(nvarchar(30), mycolumn, 126)

Or 127 if you want timezone
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 38382182
Obviously you need to select from your table:

Select convert(nvarchar(30), mycolumn, 126) from mytable

Or 127 if you want timezone

If you want to update the table and store the time in UTC format then you will need to create a new column as a nvarchar(30) field. The 30 isn't a fixed value. It could be more or less as long as it can contain the converted string length. Then you will need to update that column with a convert statement similar to the above.
0
 
LVL 25

Expert Comment

by:TempDBA
ID: 38382225
You mean to say that the column is of timestamp datatype or the datatype is datetime and you are storing getdate() in the column. If it is the latter case, you can simply update the value to utc equivalent by

update tablename
set columnname = getutcdate()
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:Danzigger
ID: 38382254
Thanks for the suggestions.  TempDBA, yes sorry, the latter is my scenario - ie. the datatype is datetime and I have been storing getdate() in the column.  I can't test your suggestion right now but wouldn't that set all values to the current UTC time?
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38382936
If I am understanding correctly ... your database has datetime stored using the getdate() value ... which is the server datetime.  You would like to update the data to be UTC based from the existing values.

First question ... is your server in a daylight saving affected zone and the datetime values stored in and out of daylight savings?
If daylight savings is not an issue ... then a simple dateadd('h', +/-n, datevalue) would probably be enough.

If however daylight saving is an issue ... things get harder.  SQL server has no facility/function to calculate the a new time from an old time by specifying a change in timezone (at least not one I am aware of).

Next question ... are we talking a lot of data?
If not too much data ... some programming languages have the ability to do to the conversions.  You may be able to utilise them.  It would be painfully slow to do it one record at a time though.

If a lot of data ... this may best be done in server.  This may need to be done via a custom function (UDF) to do the math on the datetimes.
How far back in time are the dates?  As daylight saving start and ends shift from time to time in various regions ... catering for the start/end periods for a long period may be harder.  For a full list of the historical timezone data (offsets etc), the TZ database is the best source I think you will find.  It is available here: http://www.iana.org/time-zones ... and wikipedia has an article on it here http://en.wikipedia.org/wiki/Zoneinfo which also explains the format of the data file.
0
 
LVL 25

Expert Comment

by:TempDBA
ID: 38383106
Danzigger,
     Yes that's true. So, in that case just you can make use of datediff and dateadd function in sql server.

Expert Iwadwell,
      Good suggestion.
0
 
LVL 13

Expert Comment

by:sameer2010
ID: 38385660
I would recommend using this as datatype for new column
http://msdn.microsoft.com/en-us/library/bb630289(v=sql.105).aspx

You can also write UDF that would also help in local time to UTC conversion using DATEDIFF, DATEADD.
0
 

Author Comment

by:Danzigger
ID: 38416482
Hi - thanks for the responses.  lwadwell, yes you understand correctly.  And yes to your first question, the server is in New Zealand and contains datetimes both in and out of daylight savings.

There are currently about 425,000 records.  I could write a c# script to update them - thoughts on whether that's doable on that number of records?

Thanks for the suggestion sameer2010 but I prefer the idea of storing all datetimes in UTC time - it seems safer to me.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38416492
Over what date range?  min(date) vs max(date)

For that volume ... updating row by row would take a while.
0
 

Author Comment

by:Danzigger
ID: 38416766
Min date: 2011-07-27 22:23:17.080
Max date: 2012-09-06 15:48:22.563

Thanks.
0
 
LVL 25

Accepted Solution

by:
lwadwell earned 2000 total points
ID: 38416798
That is only one rule on and off then
    # Rule      NAME      FROM      TO            TYPE            IN      ON            AT            SAVE        LETTER/S
    Rule      NZ            2007      max            -            Sep      lastSun      2:00s      1:00        D
    Rule      NZ            2008      max            -            Apr      Sun>=1      2:00s      0        S

You could do that in a CASE statement, e.g. (incomplete - check dates please)
UPDATE your_table
SET ts_column = CASE WHEN ts_column BETWEEN '2011-09-25 02:00' AND '2012-04-01 01:59:59.999' THEN datediff(h, -11, ts_column) ELSE datediff(h, -12, ts_column) END

Is it -11 in daylight saving or -13 ... my brain isn't working today.

Also double check the boundary hour/minutes etc.

Would need to be done soon as next daylight saving starts soon ... add another WHEN ... BETWEEN for this year if required.
0
 

Author Comment

by:Danzigger
ID: 38417062
Ah yes, I see what you mean - looks great, I'll give that a try.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question