首页 > 解决方案 > 如何在 postgres 中将列数据从 GMT 时区转换为本地时间?

问题描述

我有一列“last_status_change_date”,其中包含 GMT 时间戳中的数据。我需要在“GMT-6”中转换这些数据。我怎样才能做到这一点?

 select last_status_change_date from bss_calling_card where  card_sn ='030400091074'; 
          last_status_change_date
    -------------------------
     2020-01-03 17:06:51
    (1 row)

结果集应该是这样的:

last_status_change_date
        -------------------------
         2020-01-03 11:06:51
        (1 row)

标签: postgresql

解决方案


没有时区的时间戳谬误。尝试:

select (cast(last_status_change_date as timestamp with time zone) at time zone 'UTC') at time zone '-06:00' last_status_change_date from ...;

这将获取您的时间戳并将类型转换为带有时区的时间戳,然后通知 Postgres 它当前所在的时区,最后指示您想要的结果的时区。


推荐阅读