开发者

Sql Server 2008 - Convert localized DateTime column to UTC

I have some columns in a few tables in my database that are all in the localized server DateTime values. I want to convert these all to UTC DateTime values (does not need to开发者_如何学Python be DateTime2 since it will be assumed the new values will be in UTC).

I would like to do this in a single SQL statement. I am not entirely sure how to perform this as I do not spend a lot of time in the database.

Assumptions can be made that my table name is TestTable and my localized DateTime column is called TestDateTime.

I would want the script to do the update to the table as well as the conversion.

Any way to possibly do this?


You can substract getutcdate() from getdate() to find the difference:

select  DateAdd(s,DateDiff(s, getdate(),getutcdate()), LocalizedDateColumn)

This assumes you are in the same summertime/wintertime state as the time you're trying to convert. Not ideal, but the best conversion I know of that you can do in pure SQL.

A C# client has a much stronger conversion:

yourDateTime.ToUniversalTime()

Unlike the SQL version, the .NET version will correctly convert a wintertime date during the summer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜