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


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

2 comments:

Anonymous said...

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.

Suprotim Agarwal said...

Yep, Good point!