我有一个表,该表在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
答案 0 :(得分: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中找到受支持的时区列表。