从带有时区的时间戳中提取当地时间

Extract local hour from timestamp with time zone

我正在尝试从 timestamp with time zone 中提取当地时间,但由于我是使用这种数据类型的新手,所以我得到了意想不到的结果。

我期待 22 作为下面每一行的输出。

with my_data as(
   select to_timestamp_tz(ts, 'yyyy-mm-dd hh24:mi:ss tzh:tzm') as tsz
     from (select '2017-12-07 22:23:24 +' || lpad(level, 2, '0') || ':00' as ts  
             from dual connect by level <= 10
          )       
)
select dbtimezone
      ,sessiontimezone
      ,tsz
      ,extract(hour from tsz)
  from my_data;

为什么会发生这种情况,我需要做什么才能从带时区的时间戳中提取当地时间?

勾选documentation of EXTRACT(datetime):

When extracting from a datetime with a time zone value, the value returned is in UTC.

如果您想获取当地时间,请使用 TO_CHAR(tsz, 'HH24')EXTRACT(hour from cast(tsz as timestamp))