1. 程式人生 > >oracle date轉timestamp等各種轉換

oracle date轉timestamp等各種轉換

1、獲取系統時間的語句(ssxff6獲取小數點後面六位)
select sysdate,systimestamp,to_char(systimestamp, 'yyyymmdd hh24:mi:ssxff6'),
  to_char(systimestamp, 'yyyymmdd hh24:mi:ss.ff6') from dual;  

2、字元型轉成timestamp 
select to_timestamp('2011-09-14 12:52:42.123456789', 'syyyy-mm-dd hh24:mi:ss.ff') from dual;

3、timestamp轉成date型
select cast(to_timestamp('2011-09-14 12:52:42.123456789', 'syyyy-mm-dd hh24:mi:ss.ff') as date) timestamp_to_date from dual;  


4、date型轉成timestamp
select cast(sysdate as timestamp) date_to_timestamp from dual;  

5、兩date的日期相減得出的是天數,而兩timestamp的日期相減得出的是完整的年月日時分秒小數秒
select sysdate-sysdate,systimestamp-systimestamp from dual;

select extract(day from inter) * 24 * 60 * 60 +   
  extract(hour from inter) * 60 * 60 + extract(minute from inter) * 60 +   

    extract(second from inter) "seconds" from 
(
  select to_timestamp('2011-09-14 12:34:23.281000000', 'yyyy-mm-dd hh24:mi:ss.ff') -   
    to_timestamp('2011-09-14 12:34:22.984000000', 'yyyy-mm-dd hh24:mi:ss.ff') inter from dual
); 

select extract(second from to_timestamp('2011-09-14 12:34:23.281000000', 'yyyy-mm-dd hh24:mi:ss.ff'))-

  extract(second from to_timestamp('2011-09-14 12:34:22.984000000', 'yyyy-mm-dd hh24:mi:ss.ff')) from dual;

注:所以,timestamp要算出兩日期間隔了多少秒,要用函式轉換一下。

to_char函式支援date和timestamp,但是trunc卻不支援TIMESTAMP資料型別。