ORACLE数据库SQL中,我们常用的yyyy-MM-dd、yyyy/MM/dd等几种格式,Reference官方文档,并附上详细的应用示例综合成文,希望能够对大家学习和使用有所帮助。
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
SQL> SELECT SESSIONTIMEZONE FROM DUAL;
SESSIONTIMEZONE
---------------------------------------------------------------------------
+08:00
SQL> show parameter nls;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
nls_calendar string GREGORIAN
nls_comp string BINARY
nls_currency string ¥
nls_date_format string DD-MON-RR
nls_date_language string SIMPLIFIED CHINESE
nls_dual_currency string ¥
nls_iso_currency string CHINA
nls_language string SIMPLIFIED CHINESE
nls_length_semantics string BYTE
nls_nchar_conv_excp string FALSE
nls_numeric_characters string .,
nls_sort string BINARY
nls_territory string CHINA
nls_time_format string HH.MI.SSXFF AM
nls_time_tz_format string HH.MI.SSXFF AM TZR
nls_timestamp_format string DD-MON-RR HH.MI.SSXFF AM
nls_timestamp_tz_format string DD-MON-RR HH.MI.SSXFF AM TZR
SQL> select to_char(sysdate,'yyyy.mm.dd') from dual;
TO_CHAR(SYSDATE,'YYYY.MM.DD')
-----------------------------
2015.12.28
SQL> select to_char(sysdate,'yyyy+mm=dd') from dual;
TO_CHAR(SYSDATE,'YYYY+MM=DD')
-----------------------------
2015+12=28
SQL> select to_char(sysdate,'yyyy-mm-dd') from dual;
TO_CHAR(SYSDATE,'YYYY-MM-DD')
-----------------------------
2015-12-28
SQL> select to_char(sysdate,'AD yyyy-mm-dd') from dual;
TO_CHAR(SYSDATE,'ADYYYY-MM-DD'
------------------------------
公元 2015-12-28
SQL> select to_char(sysdate,'A.D. yyyy-mm-dd') from dual;
TO_CHAR(SYSDATE,'A.D.YYYY-MM-D
------------------------------
公元 2015-12-28
SQL> select to_char(sysdate,'B.C. yyyy-mm-dd') from dual;
TO_CHAR(SYSDATE,'B.C.YYYY-MM-D
------------------------------
公元 2015-12-28
SQL> select to_char(sysdate,'BC yyyy-mm-dd') from dual;
TO_CHAR(SYSDATE,'BCYYYY-MM-DD'
------------------------------
**公元** 2015-12-28
SQL> select to_char(sysdate,'YYYY-MM-DD AM hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'YYYY-MM-DDAMH
------------------------------
2015-12-27 **下午** 17:52:58
SQL> select to_char(sysdate,'YYYY-MM-DD PM hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'YYYY-MM-DDPMH
------------------------------
2015-12-27 **下午** 17:54:29
提示:
如果年份中最后两位数字在01到99(含)之间,则返回值等于年份前两位+1
如果年份中最后两位数字是00,则返回值与年份前两位相同
SQL> select to_char(sysdate,'cc YYYY-MM-DD PM hh24:mi:ss ') from dual;
TO_CHAR(SYSDATE,'CCYYYY-MM-DDP
------------------------------
**21** 2015-12-27 下午 17:57:10
SQL> select to_char(sysdate,'D YYYY-MM-DD PM hh24:mi:ss ') from dual;
TO_CHAR(SYSDATE,'DYYYY-MM-DDPM
------------------------------
1 2015-12-27 下午 18:09:43
SQL> select to_char(sysdate,'DD YYYY-MM-DD PM hh24:mi:ss ') from dual;
TO_CHAR(SYSDATE,'DDYYYY-MM-DDP
------------------------------
27 2015-12-27 下午 18:12:08
SQL> select to_char(sysdate,'DDD YYYY-MM-DD PM hh24:mi:ss ') from dual;
TO_CHAR(SYSDATE,'DDDYYYY-MM-DD
------------------------------
361 2015-12-27 下午 18:13:15
select to_char(sysdate,'Day YYYY-MM-DD PM hh24:mi:ss ') from dual;
TO_CHAR(SYSDATE,'DAYYYYY-MM-DD
-----------------------------------
星期日 2015-12-27 下午 18:46:23
SQL> select to_char(sysdate,'MON YYYY-MM-DD PM hh24:mi:ss ') from dual;
TO_CHAR(SYSDATE,'MONYYYY-MM-DD
----------------------------------
12月 2015-12-27 下午 18:47:33
SQL> select to_char(sysdate,'MONTH YYYY-MM-DD PM hh24:mi:ss ') from dual;
TO_CHAR(SYSDATE,'MONTHYYYY-MM-
--------------------------------
12月 2015-12-27 下午 18:48:44
SQL> select to_char(sysdate,'YEAR YYYY-MM-DD PM hh24:mi:ss ') from dual;
TO_CHAR(SYSDATE,'YEARYYYY-MM-D
--------------------------------------------------------------------
TWENTY FIFTEEN 2015-12-27 下午 18:52:21
SQL> select to_char(sysdate,'YYYY') from dual;
TO_CHAR(SYSDATE,'YYYY')
-----------------------
2015
SQL> select to_char(sysdate,'MM') from dual;
TO_CHAR(SYSDATE,'MM')
---------------------
12
SQL> select to_char(sysdate,'HH') from dual;
TO_CHAR(SYSDATE,'HH')
---------------------
06
select to_char(sysdate,'HH12') from dual;
TO_CHAR(SYSDATE,'HH12')
-----------------------
06
SQL> select to_char(sysdate,'HH24') from dual;
TO_CHAR(SYSDATE,'HH24')
-----------------------
18
SQL> select to_char(sysdate,'MI') from dual;
TO_CHAR(SYSDATE,'MI')
---------------------
59
SQL> select to_char(sysdate,'SS') from dual;
TO_CHAR(SYSDATE,'SS')
---------------------
13
SQL> select to_char(SYSTIMESTAMP,'FF9') from dual;
TO_CHAR(SYSTIMESTAMP,'FF9')
---------------------------
487000000
SQL> select to_char(SYSTIMESTAMP,'FF4') from dual;
TO_CHAR(SYSTIMESTAMP,'FF4')
---------------------------
4740
SQL> select to_char(SYSTIMESTAMP,'FF6') from dual;
TO_CHAR(SYSTIMESTAMP,'FF6')
---------------------------
032000
SQL> select to_char(SYSTIMESTAMP,'Y,YYY') from dual;
TO_CHAR(SYSTIMESTAMP,'Y,YYY')
-----------------------------
2,015
SQL> select to_char(to_date('198-2-1','yyyy-MM-dd'),'IYYY') from dual;
TO_CHAR(TO_DATE('198-2-1','YYY
------------------------------
0198
SQL> select to_char(sysdate,'Y') from dual;
TO_CHAR(SYSDATE,'Y')
--------------------
5
SQL> select to_char(sysdate,'YY') from dual;
TO_CHAR(SYSDATE,'YY')
---------------------
15
SQL> select to_char(sysdate,'YYY') from dual;
TO_CHAR(SYSDATE,'YYY')
----------------------
015
SQL> select to_char(sysdate,'YYYY') from dual;
TO_CHAR(SYSDATE,'YYYY')
-----------------------
2015
SQL> select to_char(sysdate,'W') from dual;
TO_CHAR(SYSDATE,'W')
--------------------
4
SQL> select to_char(sysdate,'WW YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'WWYYYY-MM-DD'
------------------------------
52 2015-12-27
SQL> select to_char(sysdate,'IW YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'IWYYYY-MM-DD'
------------------------------
52 2015-12-27
SQL> select to_char(sysdate,'RM YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'RMYYYY-MM-DD'
------------------------------
**XII** 2015-12-27
SQL> select to_char(sysdate,'Q YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'QYYYY-MM-DD')
------------------------------
**4** 2015-12-27
SQL> select to_char(sysdate,'J YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'JYYYY-MM-DD')
------------------------------
**2457384** 2015-12-27
SQL> select to_char(sysdate,'SSSSS YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'SSSSSYYYY-MM-
------------------------------
**70782** 2015-12-27
SQL> select to_char(sysdate,'DL YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'DLYYYY-MM-DD'
------------------------------------
2015年12月27日 星期日 2015-12-27
SQL> select to_char(sysdate,'DS YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'DSYYYY-MM-DD'
------------------------------
2015-12-27 2015-12-27
SQL> select to_char(sysdate,'TS YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'TSYYYY-MM-DD'
------------------------------
下午 7:44:23 2015-12-27
SQL> select to_char(sysdate,'DY YYYY-MM-DD') from dual;
TO_CHAR(SYSDATE,'DYYYYY-MM-DD'
------------------------------
星期日 2015-12-27
SQL> alter session set nls_language='AMERICAN';
Session altered
SQL> select to_char(sysdate,'dy') from dual;
TO_CHAR(SYSDATE,'DY')
---------------------
sun
如果输入的两位数是00-49
如果当前年份属于00-49,则返回成本世纪所在年份
如果当前年份属于50-99,则返回成下世纪所在年份
如果输入的两位数是50-99
如果当前年份属于00-49,则返回成上世纪所在年份
如果当前年份属于50-99,则返回成本世界所在年份
注:这种自动转换只适用于 String -> Date 的转换,如果是 Date -> String,那就按照 Date 存储的值来转换,此时再用 RR/RRRR 已经没有意义了,因为 Oracle 内部存储的年份是四位数的。
SQL> select sysdate from dual;
SYSDATE
-----------
2015/12/27
SQL> select to_date('12','rr') from dual;
TO_DATE('12','RR')
------------------
2012/12/1
SQL> select to_date('51','rr') from dual;
TO_DATE('51','RR')
------------------
1951/12/1
SQL> select to_date('97-11-3','rrrr-mm-dd') from dual;
TO_DATE('97-11-3','RRRR-MM-DD'
------------------------------
1997-11-3
SQL> select to_date('17-11-3','rrrr-mm-dd') from dual;
TO_DATE('17-11-3','RRRR-MM-DD'
------------------------------
2017-11-3
提示:FM及FX用于to_*函数中控制空白填充或精确检查,其提供的是一个格式化方案,并且具备相当的持久能力,比如当你第一次调用之后即开始生效,后续再调用时即使不指定FM/FX,也仍然会生效。当你再次调用时失效,第三次调用时又开始生效,以此类推
Oracle 默认使用空白字符填充被格式化的元素,确保宽度一致。例如:
SQL> select to_char(to_date('2007-3-1','yyyy-mm-dd'),'month') mm,length(to_char(to_date('2007-3-1','yyyy-mm-dd'),'month')) ll from dual;
MM LL
--------- ----------
march 9
SQL> select to_char(to_date('2007-12-1','yyyy-mm-dd'),'month') mm,length(to_char(to_date('2007-12-1','yyyy-mm-dd'),'month')) ll from dual;
MM LL
--------- ----------
december 9
如上例可以看到,不管几月份,其返回所占长度是一致的。
如果是字符格式化,指定fm参数后将仅返回指定属性实际所占长度(不再以空格填充)。例如:
SQL> select to_char(to_date('2007-3-1','yyyy-mm-dd'),'fmmonth') mm,length(to_char(to_date('2007-3-1','yyyy-mm-dd'),'fmmonth')) ll from dual;
MM LL
----- ----------
march 5
SQL> select to_char(to_date('2007-12-1','yyyy-mm-dd'),'fmmonth') mm,length(to_char(to_date('2007-12-1','yyyy-mm-dd'),'fmmonth')) ll from dual;
MM LL
-------- ----------
december 8
与上对比,看出区别来了吧。如果是数值格式化,fm参数会自动舍弃被格式化元素的前置0。例如:
SQL> alter session set nls_date_format='fmyyyy-mm-dd hh24:mi:ss';
Session altered.
SQL> select to_date('2007-8-1 03:03:04','yyyy-mm-dd hh24:mi:ss') from dual;
TO_DATE('2007-8-103
-------------------
2007-8-1 3:3:4
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
SQL> select to_date('2007-8-1 03:03:04','yyyy-mm-dd hh24:mi:ss') from dual;
TO_DATE('2007-8-103
-------------------
2007-08-01 03:03:04
SQL> alter session set nls_date_format='fmyyyy-mm-fmdd hh24:fmmi:ss';
Session altered.
SQL> select to_date('2007-8-1 03:03:04','yyyy-mm-dd hh24:mi:ss') from dual;
TO_DATE('2007-8-103
-------------------
2007-8-01 03:3:4
使用FX参数之后:
字符及对应格式必须严格一一对应,甚至连分隔符也需要相符。
不允许有多余空格。
数值参与格式需要完全对应(或通过fm参数自动补足0)
SQL> select to_date('2007-9-26','yyyy-mm-dd') from dual;
TO_DATE('2007-9-26'
-------------------
2007-09-26 00:00:00
SQL> select to_date('2007-9-26','fxyyyy-mm-dd') from dual;
select to_date('2007-9-26','fxyyyy-mm-dd') from dual
*
ERROR at line 1:
ORA-01862: the numeric value does not match the length of the format item
SQL> select to_date('2007-09-26','fxyyyy-mm-dd') from dual;
TO_DATE('2007-09-26
-------------------
2007-09-26 00:00:00
SQL> select to_date('2007-9-26','fxyyyy-fmmm-dd') from dual;
TO_DATE('2007-9-26'
-------------------
2007-09-26 00:00:00
SQL> select to_date('2007-9-26','fxyyyy/fmmm/dd') from dual;
select to_date('2007-9-26','fxyyyy/fmmm/dd') from dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL> select to_char(systimestamp,'hh24:mi:ssxff') from dual;
TO_CHAR(SYSTIMESTAMP,'HH24:MI:
------------------------------
16:21:12.609000
以下四项为时区相关参数, 明确表示某一个时区,应该联合使用 TZH TZM 或者 TZR TZD
TZD /TZR
SQL> select to_char(systimestamp,'tzr:tzd') from dual;
TO_CHAR(SYSTIMESTAMP,'TZR:TZD')
---------------------------------------
+08:00
TZH/TZM
SQL> select to_char(systimestamp,'tzh:tzm') from dual;
TO_CHA
------
+08:00
作者 @zokaper
2015 年 12月 27日
[1] http://www.5ienet.com/note/html/numdate/oracle-date-time-format(1).shtml