Convert existing datetimes to UTC

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!
DanziggerAsked:
Who is Participating?
 
lwadwellCommented:
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
 
Lee SavidgeCommented:
Select convert(nvarchar(30), mycolumn, 126)

Or 127 if you want timezone
0
 
Lee SavidgeCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
TempDBACommented:
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
 
DanziggerAuthor Commented:
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
 
lwadwellCommented:
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
 
TempDBACommented:
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
 
sameer2010Commented:
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
 
DanziggerAuthor Commented:
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
 
lwadwellCommented:
Over what date range?  min(date) vs max(date)

For that volume ... updating row by row would take a while.
0
 
DanziggerAuthor Commented:
Min date: 2011-07-27 22:23:17.080
Max date: 2012-09-06 15:48:22.563

Thanks.
0
 
DanziggerAuthor Commented:
Ah yes, I see what you mean - looks great, I'll give that a try.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.