确定 T-SQL 中的时区偏移 [英] Determine Time Zone Offset in T-SQL

查看:35
本文介绍了确定 T-SQL 中的时区偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库应用程序将部署在不同时区的多个站点.

My database application is going to be deployed at multiple sites in different time zones.

我需要一个 T-SQL 函数来确定当年 1 月 1 日午夜的 UTC 时间戳以进行 YTD 计算.所有数据都存储在 UTC 时间戳中.

I need a T-SQL function that will determine the UTC timestamp of midnight on January 1 of the current year for YTD calculations. All of the data is stored in UTC timestamps.

例如,芝加哥是 UTC-6 夏令时 (DST),如果在 2008 年芝加哥的任何时间运行,该函数需要返回 '2008-01-01 06:00:00'.如果明年在纽约(GMT-5 + DST)运行,则需要返回'2009-01-01 05:00:00'.

For example, Chicago is UTC-6 with Daylight Savings Time (DST), the function needs to return '2008-01-01 06:00:00' if run any time in 2008 in Chicago. If run in New York (GMT-5 + DST) next year, it needs to return '2009-01-01 05:00:00'.

我可以从 YEAR(GETDATE()) 获取当前年份.我以为我可以在 GETDATE()GETUTCDATE() 之间做一个 DATEDIFF 来确定偏移量,但结果取决于查询是否运行在 DST 期间与否.我不知道有任何内置的 T-SQL 函数来确定偏移量或当前时间是否为夏令时?

I can get the current year from YEAR(GETDATE()). I thought I could do a DATEDIFF between GETDATE() and GETUTCDATE() to determine the offset but the result depends on whether the query is run during DST or not. I do not know of any built in T-SQL functions for determining the offset or whether or not the current time is DST or not?

有没有人在 T-SQL 中有解决这个问题的方法?我可以对其进行硬编码或将其存储在表中,但我不想这样做.我想这是在 SQL Server 2005 中使用 CLR 集成的完美情况.我只是想知道是否有我不知道的 T-SQL 解决方案?

Does anyone have a solution to this problem in T-SQL? I could hard code it or store it in a table but would prefer not to. I suppose that this is a perfect situation for using CLR Integration in SQL Server 2005. I am just wondering if there is a T-SQL solution that I am unaware of?

推荐答案

查看之前的问答以获取相关信息:

Check out this previous question and answer for related information:

有效在 SQL 2005 中转换 UTC 和本地(即 PST)时间之间的日期

(总而言之,您确实需要在 Sql Server 2005 中构建时区和 DST 表.在下一个版本的 Sql Server 中,我们将获得有关时区的一些帮助.)

(To summarize, you do need to build time zone and DST tables in Sql Server 2005. In the next version of Sql Server we get some help with time zones.)

这篇关于确定 T-SQL 中的时区偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆