0

我想学习时区转换我尝试了此代码进行时区转换

select 
    ( TO_TIMESTAMP_TZ 
         ( TO_CHAR
             ( sysdate, 'YYYY/MM/DD HH24:MI:SS' ) 
             || ' GMT' ,   'YYYY/MM/DD HH24:MI:SS TZR' 
         ) 
    AT TIME ZONE  DECODE
        (1,1,'US/PACIFIC',2,'EUROPE/LONDON',3,'ASIA/TOKYO',4,'ASIA/CALCUTTA');

但相反,如果我尝试从表中获取时区而不是使用解码语句,我会收到错误

select
    (TO_TIMESTAMP_TZ 
        ( TO_CHAR 
            ( sysdate, 'YYYY/MM/DD HH24:MI:SS' ) 
            || ' GMT' , 'YYYY/MM/DD HH24:MI:SS TZR' 
         ) 
    AT TIME ZONE 
        (select t_zone from table where region_code=1);

你能帮我理解是什么问题吗。

我什至尝试过new_time(),但还是不行

4

1 回答 1

0

首先,您发布的所有查询都缺少FROM类似的子句FROM dual;,因此无论如何它们都会出错。以下是我的结果-

SQL> ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY HH24:MI:SS';

Session altered.

SQL> select TO_TIMESTAMP_TZ (sysdate,'YYYY-MM-DD HH24.MI.SSXFF TZR') from dual;

TO_TIMESTAMP_TZ(SYSDATE,'YYYY-
---------------------------------------------------------------------------
20-AUG-20 12.13.15 PM -05:00

SQL> select from_tz(to_timestamp(sysdate,'DD-MON-YYYY HH24:MI:SS'), 'US/Eastern') 
at time zone 'Turkey' from dual;

FROM_TZ(TO_TIMESTAMP(SYSDATE,'
---------------------------------------------------------------------------
20-AUG-12 08.15.20 PM TURKEY

SQL> create table tzone (tm_zone varchar2(30));
Table created.

SQL> insert into tzone values('Turkey');

SQL> select * from tzone;

TM_ZONE
------------------------------
Turkey

SQL> select from_tz(to_timestamp(sysdate,'DD-MON-YYYY HH24:MI:SS'), 'US/Eastern') at time zone (select tm_zone from tzone) from dual;

FROM_TZ(TO_TIMESTAMP(SYSDATE,'
---------------------------------------------------------------------------
20-AUG-12 08.16.29 PM TURKEY
于 2012-08-20T18:17:54.917 回答