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','NLS_DATE_LANGUAGE = American') from dual;
monday
設置日期語言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
也可以這樣
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_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 NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
或者在to_date中寫
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
注意我這只是舉了NLS_DATE_LANGUAGE,當然還有很多,
可查看
select * from nls_session_parameters
select * from V$NLS_PARAMETERS
日期和字符轉換函數用法(to_date,to_char)
We overwrite NLS_DATE_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
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_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