在Oracle中實現各種日期處理(1)

jss001發表於2009-02-11
TO_DATE格式
  Day:   
  dd    number     12
  dy    abbreviated  fri     
  day   spelled out  friday            
  ddspth  spelled out,  ordinal  twelfth
  Month:  
  mm    number     03
  mon   abbreviated  mar
  month  spelled out  march 
  Year:             
  yy    two digits  98
  yyyy   four digits  1998  
  
  24小時格式下時間範圍為: 0:00:00 - 23:59:59....
  12小時格式下時間範圍為: 1:00:00 - 12:59:59 ....  
1.
  日期和字元轉換函式用法(to_date,to_char)   
 
2.
  select to_char( to_date(222,'J'),'Jsp') from dual
  
  顯示Two Hundred Twenty-Two
 
3.
  求某天是星期幾
  select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; 
  星期一
  select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NL2006-3-22_LANGUAGE = American') from dual; 
  monday  
  設定日期語言
  ALTER SESSION SET NL2006-3-22_LANGUAGE='AMERICAN';
  也可以這樣
  TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NL2006-3-22_LANGUAGE = American')
 
4.
  兩個日期間的天數
  select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
 
5.   時間為null的用法
  select id, active_date from table1
   UNION
  select 1, TO_DATE(null) from dual;
  
  注意要用TO_DATE(null)
 
6. 
  a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
  那麼12月31號中午12點之後和12月1號的12點之前是不包含在這個範圍之內的。
  所以,當時間需要精確的時候,覺得to_char還是必要的
7.   日期格式衝突問題
     輸入的格式要看你安裝的ORACLE字符集的型別, 比如: US7ASCII, date格式的型別就是: '01-Jan-01'
  alter system set NL2006-3-22_LANGUAGE = American
     alter session set NL2006-3-22_LANGUAGE = American
  或者在to_date中寫
  select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NL2006-3-22_LANGUAGE = American') from dual; 
  注意我這只是舉了NL2006-3-22_LANGUAGE,當然還有很多,
  可檢視
  select * from nls_session_parameters
  select * from V$NLS_PARAMETERS
 

 日期和字元轉換函式用法(to_date,to_char)
     We overwrite NL2006-3-22_FORMAT into different formats for the session. 
     SQL> alter session set nls_date_format = 'DD-MON-RR'; Session altered.
     SQL> set pagesize 0
   SQL> set linesize 130
   SQL> select * from nls_session_parameters;
   NLS_LANGUAGE          AMERICAN
   NLS_TERRITORY         AMERICA
   NLS_CURRENCY          $
   NLS_ISO_CURRENCY        AMERICA
   NLS_NUMERIC_CHARACTERS     .,
   NLS_CALENDAR          GREGORIAN
   NL2006-3-22_FORMAT        DD-MON-RR
   NL2006-3-22_LANGUAGE       AMERICAN
   NLS_SORT            BINARY
   NLS_TIME_FORMAT        HH.MI.SSXFF AM
   NLS_TIMESTAMP_FORMAT      DD-MON-RR HH.MI.SSXFF AM
   NLS_TIME_TZ_FORMAT       HH.MI.SSXFF AM TZH:TZM
   NLS_TIMESTAMP_TZ_FORMAT    DD-MON-RR HH.MI.SSXFF AM TZH:TZM
   NLS_DUAL_CURRENCY       $
   NLS_COMP            BINARY

   15 rows selected.
     specify it in SQL statement:
     
    SQL> select to_date('03-SEP-1999','DD-MON-YYYY') from dual;
     03-SEP-99
    SQL> alter session set nls_date_format = 'MM-DD-YYYY';
       Session altered.
      
    SQL> select to_date('03-SEP-99','DD-MON-YY') from dual;
       TO_DATE('0
       ----------
       09-03-1999 
    SQL> alter session set nls_date_format = 'RRRR-MM-DD';
       Session altered. 
      
    SQL> select to_date('03-SEP-1999','DD-MON-YYYY') from dual;
       TO_DATE('0
       ----------
       1999-09-03 
      
  When we use TO_CHAR function, we get expected results of format from current SQL statement: 
    SQL> alter session set nls_date_format = 'MM-DD-YY';
       Session altered. 
    SQL> select to_char(sysdate,'dd-mm-yyyy') from dual;
       TO_CHAR(SYSDATE,'DD-MM-YYYY') 
       07-09-1999 
    SQL> alter session set nls_date_format = 'RR-MON-DD';
       Session altered. 
    SQL> select to_char(sysdate,'dd-mon-yy') from dual;
       TO_CHAR(SYSDATE,'DD-MON-YY') 
       07-sep-99 
    SQL> select to_char(sysdate,'dd-Mon-yy') from dual;
       TO_CHAR(SYSDATE,'DD-MON-YY') 
       07-Sep-99
8.
  select count(*) 
  from ( select rownum-1 rnum 
     from all_objects 
     where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-
     02-01','yyyy-mm-dd')+1 
     ) 
  where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )
  not 
  in ( '1', '7' )
  
  查詢2002-02-28至2002-02-01間除星期一和七的天數
  在前後分別呼叫DBMS_UTILITY.GET_TIME, 讓後將結果相減(得到的是1/100秒, 而不是毫秒).  
 

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18921899/viewspace-1017195/,如需轉載,請註明出處,否則將追究法律責任。

相關文章