【问题标题】:Convert different timezones to UTC Date将不同时区转换为 UTC 日期
【发布时间】:2020-04-07 03:36:00
【问题描述】:

我有一个在 SQL Server 表中包含日期时间列和时区列的表。我现在正在尝试将不同时区的日期时间转换为 UTC 日期,并将数据加载到新表中并删除时区列。

例如:

Date time.              TIMEZONE
1/1/2020 10:34 AM       EST
1/2/2020 08:26 AM.      CST 

新表:

Date time 
1/1/2020 6:34 pm
1/2/2020 4:26 pm

【问题讨论】:

  • 为什么还要标记 MySQL?
  • 注意时区缩写。如果您的数据仅限于美国,您可以推断CST 表示“中部标准时间”。但如果不是,那么你必须有其他方法来消除“古巴标准时间”和“中国标准时间”的歧义,它们都有缩写“CST”。

标签: sql-server timezone


【解决方案1】:

您的数据几乎可以按原样使用。

create table #d (
    dt datetime not null,
    tz char(3) not null
);

insert into #d (dt, tz)
values 
    ('1/1/2020 10:34 AM', 'EST'),
    ('1/2/2020 08:26 AM', 'CST');

create table #tz (
    tz char(3), 
    long_tz sysname not null
)

insert into #tz (tz, long_tz)
values 
    ('EST', 'Eastern Standard Time'),
    ('CST', 'Central Standard Time');

select #d.*, #d.dt at time zone #tz.long_tz at time zone 'UTC'
from #d
join #tz
    on #d.tz = #tz.tz;

为了详细说明这里发生了什么,我使用了两次at time zone 子句 - 一次告诉数据库数据已经记录在哪个时区,然后第二次将其转换为 UTC。

您可以在sys.time_zone_info 中找到支持的时区列表。

【讨论】:

    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多