views:

189

answers:

1

I have recently updated my system to record date/times as UTC as previously they were storing as local time.

I now need to convert all the local stored date/times to UTC. I was wondering if there is any built in function, similar to .NET's ConvertTime method?

I am trying to avoid having to write a utility app to do this for me.

Any suggestions?

+2  A: 

If they're all local to you, then here's the offset:

SELECT GETDATE() AS CurrentTime, GETUTCDATE() AS UTCTime

and you should be able to update all the data using:

UPDATE SomeTable
   SET DateTimeStamp = DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), DateTimeStamp)

Would that work, or am I missing another angle of this problem?

rwmnau
Looks the part, will give it a go
James
I am getting `Argument data type datetime is invalid for argument 2 of dateadd function.` Any Ideas?
James
@James: you need to reverse the second and third parameter in the call: `DateTimeStamp = DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), DateTimeStamp)` -- the second parameter is the interval (the number of hours to add or subtract), and the third is the value to apply that interval to
marc_s
Spot on @marc_s thanks!!
James
@James and Marc_s: Crap - I always get those backwards. Marc is correct - it's DATEADD(interval, AmountToAdd, startingDatetime). I've edited the answer.
rwmnau