Athena中UTC时间戳转EST并去时区转timestamp类型问题
UTC转EST时区并转为无时区Timestamp的正确写法
问题原因
你之前的SQL逻辑顺序错误:
- 若原列是无时区的timestamp类型但存储的是UTC时间,直接执行
datetimestamp_column AT TIME ZONE 'US/Eastern'会把该列值当作EST时间来转换为UTC时区的timestamptz,最终CAST得到的还是UTC时间,完全和需求反向。
正确SQL写法
场景1:原列是无时区的timestamp(存储UTC时间)
CAST( (datetimestamp_column AT TIME ZONE 'UTC') AT TIME ZONE 'US/Eastern' AS timestamp )
分步说明:
datetimestamp_column AT TIME ZONE 'UTC':先将无时区的timestamp标记为UTC时区的带时区时间(timestamptz类型)AT TIME ZONE 'US/Eastern':将UTC时区的时间转换为EST时区的无时区timestamp- 外层CAST是冗余的(因为第二步已经返回timestamp),可以简化为:
(datetimestamp_column AT TIME ZONE 'UTC') AT TIME ZONE 'US/Eastern'
场景2:原列是带时区的timestamptz(存储UTC时间)
直接转换即可,无需额外标记原时区:
datetimestamp_column AT TIME ZONE 'US/Eastern'
该语句会直接返回EST时区的无时区timestamp,CAST操作同样冗余。
验证示例
假设原列值为2024-05-20 12:00:00(UTC时间):
- 错误写法返回:
2024-05-20 16:00:00(把原时间当作EST转成UTC) - 正确写法返回:
2024-05-20 08:00:00(UTC转EST,夏令时时差4小时)
内容的提问来源于stack exchange,提问作者CWZY
相关产品推荐
相关产品推荐

