Maison > Article > base de données > ORACLE单行函数与多行函数之四:日期函数示例
实验环境 : BYS@bys1select * from nls_session_parameters where parameter='NLS_DATE_FORMAT'; PARAMETER VALUE -------------------- ------------------------------ NLS_DATE_FORMAT yyyy/mm/dd hh24:mi:ss BYS@bys1show parameter nls_lang NAME TYPE
实验环境:BYS@bys1>select * from nls_session_parameters where parameter='NLS_DATE_FORMAT';
PARAMETER VALUE
-------------------- ------------------------------
NLS_DATE_FORMAT yyyy/mm/dd hh24:mi:ss
nls_language string AMERICAN
日期+或-1,都代表加减一天的时间;而如果是一小时或几分钟这种,可以用天/小时这种方法。
如下面语句,1小时是1/24;5分钟是1/24/12。86400:1天=24小时=24*60*60=86400秒
BYS@bys1>select sysdate+365,sysdate-1,sysdate-3,sysdate-1/24,sysdate-1/24/12 from dual;2014/11/02 19:26:15 2013/11/01 19:26:152013/10/30 19:26:15 2013/11/0218:26:15 2013/11/0219:21:15
SYSTIMESTAMP返回的是TIMESTAMP WITH TIME ZONE 类型的数据。+08:00表示当前是东八区。
BYS@bys1>select systimestamp from dual;
SYSTIMESTAMP
---------------------------------------------------------------------------
02-NOV-13 09.08.04.390741 PM +08:00
timestamp的显示格式不同于SYSDATE,要重新指定。
BYS@bys1>alter session set nls_timestamp_format='yyyy-mm-dd hh24:mi:ss.ff';
Session altered.
BYS@bys1>select systimestamp from dual;
SYSTIMESTAMP
---------------------------------------------------------------------------
02-NOV-13 09.11.19.258161 PM +08:00
表示TIMESTAMP的方法:
–to_timestamp('2013-02-09 23:59:59.000','yyyy-mm-dd hh24:mi:ss.ff')
–timestamp '2013-04-05 13:48:00.123456789'
–to_timestamp中的分隔符可以更换, timestamp中的日期分隔符必须是-,时间必须是:,秒后面必须跟上.
–timestamp可以精确表示到毫秒、微秒甚至纳秒级别
转换时未指定值时的默认值:年:同SYSDATE里的年;月:同SYSDATE里的月;日:1号;时分秒和纳秒:均为0
BYS@bys1>col a3 for a30
BYS@bys1>col a2 for a30
BYS@bys1>col a1 for a30
BYS@bys1>select to_timestamp('05 13','YY HH24') as a1,to_timestamp('05 13','mm mi') as a2,to_timestamp('05 13','dd ss') as a3 from dual;
A1 A2 A3
------------------------------ ------------------------------ ------------------------------
2005-11-01 13:00:00.000000000 2013-05-01 00:13:00.000000000 2013-11-05 00:00:13.000000000
关于微秒的指定方式:FF5表示给的时间戳可以有不超过5位的微秒。如果时间戳微秒有3位,指定转换为FF2,则报错。
同时在秒后最多只能指定9位。
BYS@bys1>select to_timestamp('05 13:48:22.778','DD HH24:MI:SS.FF5') from dual;
TO_TIMESTAMP('0513:48:22.778','DDHH24:MI:SS.FF5')
---------------------------------------------------------------------------
2013-11-05 13:48:22.778000000
要注意
BYS@bys1>select to_timestamp('05 13:48:22.778','DD HH24:MI:SS.FF2') from dual;
select to_timestamp('05 13:48:22.778','DD HH24:MI:SS.FF2') from dual
*
ERROR at line 1:
ORA-01880: the fractional seconds must be between 0 and 999999999
BYS@bys1>select to_timestamp('05 13:48:22.123456789','DD HH24:MI:SS.FF9') from dual;
TO_TIMESTAMP('0513:48:22.123456789','DDHH24:MI:SS.FF9')
---------------------------------------------------------------------------
2013-11-05 13:48:22.123456789
BYS@bys1>select to_timestamp('05 13:48:22.1234567890','DD HH24:MI:SS.FF9') from dual;
select to_timestamp('05 13:48:22.1234567890','DD HH24:MI:SS.FF9') from dual
*
ERROR at line 1:
ORA-01830: date format picture ends before converting entire input string
BYS@bys1>select to_timestamp('05 13:48:22.1234567890','DD HH24:MI:SS.FF10') from dual;
select to_timestamp('05 13:48:22.1234567890','DD HH24:MI:SS.FF10') from dual
*
ERROR at line 1:
ORA-01821: date format not recognized
默认值:年:同SYSDATE里的年;月:同SYSDATE里的月;日:1号;时分秒:均为0
注意BETWEEN AND 相当于大于等于和小于等于。所以属于某一天,严格来说应该是从当天0点的0秒到 当天23:59:59秒。1天除以86400即1秒
BYS@bys1> select 'TRUE' from dual where to_date('2013-11-02 21:48:22','YYYY-MM-DD HH24:MI:SS') between date'2013-11-01' and date'2013-11-06'-1/86400;
'TRU
----
TRUE
BYS@bys1>select 'TRUE' from dual where to_date('2013/11/02 21:45:43','YYYY-MM-DD HH24:MI:SS') between to_date('2013-11-02','yyyy-mm-dd') and to_date('2013-11-03','yyyy-mm-dd hh24:mi:ss')-1/86400;
'TRU
----
TRUE
也可以用to_date对日期进行显式转换。
select 'TRUE' from dual where to_date('2013/11/02 21:45:43','YYYY-MM-DD HH24:MI:SS') between to_date('2013-11-02','yyyy-mm-dd') and to_date('2013-11-03','yyyy-mm-dd hh24:mi:ss')-1/86400;
'TRUE'
------
TRUE
注意BETWEEN AND 相当于大于等于和小于等于
BYS@bys1> select 'TRUE' from dual where to_date('2013/11/02 21:45:43','YYYY-MM-DD HH24:MI:SS') >= date'2013-11-02' and to_date('2013/11/02 21:45:43','YYYY-MM-DD HH24:MI:SS')
'TRU
----
TRUE
其实也可以用小于11月3号来表示小于等于11月2号的23:59:59秒。
BYS@bys1> select 'TRUE' from dual where to_date('2013/11/02 23:59:59','YYYY-MM-DD HH24:MI:SS') >= date'2013-11-02' and to_date('2013/11/02 23:59:59','YYYY-MM-DD HH24:MI:SS')
'TRU
----
TRUE
2013/12/02 18:39:23 2014/03/02 18:39:23
2013/11/03 19:34:20 2013/11/05 19:34:20
2013/11/30 18:43:16
ORA-01899: bad precision specifier