SQL Server Admin
T-SQL Articles

June 17, 2010

Replace SQL Server DateTime records of a column with UTC time




Here’s how to convert a DateTime column record with UTC time

-- SAMPLE DATA
DECLARE @TT TABLE (EID int, CheckIn DATETIME)
INSERT INTO @TT VALUES (2, 'April 02, 2010 11:25am')
INSERT INTO @TT VALUES (4, 'April 03, 2010 9:55am')
INSERT INTO @TT VALUES (5, 'April 07, 2010 11:24am')
INSERT INTO @TT VALUES (2, 'April 10, 2010 11:22am')
INSERT INTO @TT VALUES (3, 'April 15, 2010 5:27am')

-- QUERY
UPDATE @TT
SET CheckIn = DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), CheckIn)

SELECT * FROM @TT

OUTPUT

Before

image

After

image

You can also check my post UTC or GMT time in SQL Server 2005/2008


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



 
  Feedback:

comments

2 Responses to "Replace SQL Server DateTime records of a column with UTC time"
  1. Anonymous said...
    June 26, 2011 12:25 PM

    This of course does not account for time zones with Daylight Savings Time, which would need to be handled with a subquery to a DST data table to get the UTC offset for the original date.

  2. Suprotim Agarwal said...
    June 26, 2011 9:28 PM

    Yep, Good point!

 

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