在Oracle中使用from_tz時避免夏令時造成的異常

來源:互聯網
上載者:User

Oracle 中的from_tz函數,可以將一個timstamp和timzone拼成一個timestamp with timezoneSql代碼

  1. SQL> select from_tz(to_timestamp('20090102','YYYYMMDD'), 'America/Sao_Paulo') from dual;  
  2.    
  3. FROM_TZ(TO_TIMESTAMP('20090102  
  4. --------------------------------------------------------------------------------  
  5. 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代碼
  1. SQL> select from_tz(to_timestamp('20091101','YYYYMMDD'), 'America/Sao_Paulo') from dual;  
  2.    
  3. select from_tz(to_timestamp('20091101','YYYYMMDD'), 'America/Sao_Paulo') from dual  
  4.    
  5. 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代碼
  1. SQL> select from_tz(to_timestamp('20091101','YYYYMMDD'), tz_offset('America/Sao_Paulo')) at TIME ZONE 'America/Sao_Paulo' from dual;  
  2.    
  3. FROM_TZ(TO_TIMESTAMP('20091101  
  4. --------------------------------------------------------------------------------  
  5. 31-10月-09 11.00.00.000000000 下午 AMERICA/SAO_PAULO 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.