使用 TSQL 进行时区转换

发布于 2024-11-18 19:56:07 字数 129 浏览 2 评论 0原文

我收到每小时传入的数据(跨越 5 年),并带有以 CST 或 EST 记录的时间戳。我想以 UTC 时间将其存储在我的数据库中。

有没有办法使用 TSQL 将 CST/CDT/CPT 或 EST/EDT/EPT 转换为 UTC?

I have incoming hourly data (spanning 5 years) with timestamps recorded in CST or EST. I want to store this in my database in UTC time.

Is there a way for me to convert from CST/CDT/CPT or EST/EDT/EPT to UTC using TSQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

另类 2024-11-25 19:56:07

我建议您将它们存储为 DATETIMEOFFSET 以保留时区信息。

如果您需要显示为 UTC 日期,那么您可以使用 SWITCHOFFSET

mI would recommend you store these as DATETIMEOFFSET to preserve the timezone information.

If you need to display then as UTC Dates then you can use SWITCHOFFSET

怂人 2024-11-25 19:56:07

您可以确定行中的日期时间是 EST 还是 EDT:

自 2007 年起,当地时间更改为
东部夏令时间第二天 02:00 至东部夏令时间 03:00
三月星期日 02:00 返回
美国东部时间 (EDT) 至第一个星期日 01:00 EST
11 月,在美国以及
加拿大

然后应用 DATEADD()

Mb 我的源代码更好地解释了我的意思:

declare @t table(dt datetime)

insert @t values ('2011-07-06T10:00:00'), ('2011-01-01T00:00:00'), ('2011-03-12T00:00:00'),
    ('2006-07-06T10:00:00')

select b.dt
    , CASE
        WHEN b.dt between b.[edt_start] and [edt_end] 
            THEN DATEADD(HH, -5, b.dt)
        ELSE DATEADD(HH, -4, b.dt)
    END

    , CASE
        WHEN b.dt between b.[edt_start] and [edt_end] 
            THEN '-05:00'
        ELSE '-04:00'
    END

from
(
    select a.dt
        , DATEADD(HH, 2, CASE 
            WHEN DATEPART(WEEKDAY, a.march) = 1 
                THEN a.march 
            ELSE DATEADD(DAY, 15 - DATEPART(WEEKDAY, a.march), a.march) 
        END) [edt_start]

        , DATEADD(HH, 2, CASE 
            WHEN DATEPART(WEEKDAY, a.november) = 1 
                THEN a.march 
            ELSE DATEADD(DAY, 8 - DATEPART(WEEKDAY, a.november), a.november) 
        END) [edt_end]
    from
    (
        select t.dt
            , YEAR(t.dt) [year]
            , CAST(CAST(YEAR(t.dt) as varchar(4)) + '03' + '01' as datetime) [march]
            , CAST(CAST(YEAR(t.dt) as varchar(4)) + '11' + '01' as datetime) [november]
        from @t t
    )a
)b

You can determine is datetime in row in EST or EDT:

Since 2007, the local time changes at
02:00 EST to 03:00 EDT on the second
Sunday in March and returns at 02:00
EDT to 01:00 EST on the first Sunday
in November, in the U.S. as well as in
Canada
.

Then apply DATEADD()

Mb my source code better explains what I mean:

declare @t table(dt datetime)

insert @t values ('2011-07-06T10:00:00'), ('2011-01-01T00:00:00'), ('2011-03-12T00:00:00'),
    ('2006-07-06T10:00:00')

select b.dt
    , CASE
        WHEN b.dt between b.[edt_start] and [edt_end] 
            THEN DATEADD(HH, -5, b.dt)
        ELSE DATEADD(HH, -4, b.dt)
    END

    , CASE
        WHEN b.dt between b.[edt_start] and [edt_end] 
            THEN '-05:00'
        ELSE '-04:00'
    END

from
(
    select a.dt
        , DATEADD(HH, 2, CASE 
            WHEN DATEPART(WEEKDAY, a.march) = 1 
                THEN a.march 
            ELSE DATEADD(DAY, 15 - DATEPART(WEEKDAY, a.march), a.march) 
        END) [edt_start]

        , DATEADD(HH, 2, CASE 
            WHEN DATEPART(WEEKDAY, a.november) = 1 
                THEN a.march 
            ELSE DATEADD(DAY, 8 - DATEPART(WEEKDAY, a.november), a.november) 
        END) [edt_end]
    from
    (
        select t.dt
            , YEAR(t.dt) [year]
            , CAST(CAST(YEAR(t.dt) as varchar(4)) + '03' + '01' as datetime) [march]
            , CAST(CAST(YEAR(t.dt) as varchar(4)) + '11' + '01' as datetime) [november]
        from @t t
    )a
)b
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文