Sql Server 2008 - 将本地化日期时间列转换为 UTC
我的数据库中的一些表中有一些列,这些列全部位于本地化服务器日期时间值中。我想将这些全部转换为 UTC DateTime 值(不需要是 DateTime2,因为假定新值采用 UTC)。
我想在单个 SQL 语句中完成此操作。我不完全确定如何执行此操作,因为我没有在数据库上花费大量时间。
可以假设我的表名称为 TestTable,本地化的 DateTime 列名为 TestDateTime。
我希望脚本能够对表进行更新以及转换。
有什么办法可以做到这一点吗?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从
getdate()
中减去getutcdate()
来找出差异:这假设您与尝试转换的时间处于相同的夏令时/冬令时状态。虽然不理想,但这是我所知道的可以在纯 SQL 中完成的最佳转换。
AC# 客户端具有更强的转换功能:
与 SQL 版本不同,.NET 版本将在夏季正确转换冬季日期。
You can substract
getutcdate()
fromgetdate()
to find the difference: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:
Unlike the SQL version, the .NET version will correctly convert a wintertime date during the summer.