Oracle 中的from_tz函數,可以將一個timstamp和timzone拼成一個timestamp with timezoneSql代碼
- SQL> select from_tz(to_timestamp('20090102','YYYYMMDD'), 'America/Sao_Paulo') from dual;
-
- FROM_TZ(TO_TIMESTAMP('20090102
- --------------------------------------------------------------------------------
- 02-1月 -09 12.00.00.000000000 上午 AMERICA/SAO_PAULO
SQL> select from_tz(to_timestamp('20090102','YYYYMMDD'), 'America/Sao_Paulo') from dual; FROM_TZ(TO_TIMESTAMP('20090102--------------------------------------------------------------------------------02-1月 -09 12.00.00.000000000 上午 AMERICA/SAO_PAULO
但如果這個timestamp在指定的時區內不存在(比如由於夏令時,時鐘往前撥了一個小時),就會拋ExceptionSql代碼
- SQL> select from_tz(to_timestamp('20091101','YYYYMMDD'), 'America/Sao_Paulo') from dual;
-
- select from_tz(to_timestamp('20091101','YYYYMMDD'), 'America/Sao_Paulo') from dual
-
- ORA-01878: 在日期時間或間隔中沒有找到指定的欄位
SQL> select from_tz(to_timestamp('20091101','YYYYMMDD'), 'America/Sao_Paulo') from dual; select from_tz(to_timestamp('20091101','YYYYMMDD'), 'America/Sao_Paulo') from dual ORA-01878: 在日期時間或間隔中沒有找到指定的欄位
解決辦法嘛,先取出時區的offset,用其構建好timestamp with timezone後再轉回指定的時區就好了Sql代碼
- SQL> select from_tz(to_timestamp('20091101','YYYYMMDD'), tz_offset('America/Sao_Paulo')) at TIME ZONE 'America/Sao_Paulo' from dual;
-
- FROM_TZ(TO_TIMESTAMP('20091101
- --------------------------------------------------------------------------------
- 31-10月-09 11.00.00.000000000 下午 AMERICA/SAO_PAULO