日期类型变量的定义:to_date()函数
select to_date('2018年11月9日 23时24分24秒','yyyy"年"mm"月"dd"日" hh24"时"mi"分"ss"秒"')
from dual;
------日期类单行函数
常规日期函数:
– 取日期值得某一部分
– 日期加减
– 日期舍入
时区类日期函数
1.取日期值得某一部分
– to_char函数
select to_char(sysdate,'ss') from dual;
– extract()函数
会把date类型的变量处理成“年月日”.
SELECT extract(DAY FROM TO_DATE('2013-01-03 12:26:22','yyyy-mm-dd hh24-mi-ss')) FROM DUAL
timestamp类型变量可以提取HOUR,MINUTE,SECOND.
SELECT extract(second from TO_timestamp('2013-01-03 12:26:22','yyyy-mm-dd hh24-mi-ss')) FROM DUAL
2.日期加法
直接加上数字
select date'2013-01-01'+29+1/12 from dual
add_months()函数
add_months(date'2012-02-29',24)=add_months(date'2012-02-28',24)
interval()
select date'2017-01-01'+interval'1-6' year(3) to month from dual
select date'2013-04-17'+interval'1 23'day to hour from dual
NUMTOYMINTERVAL 和 NUMTODSINTERVAL
select date'2013-01-28'+numtoyminterval(1,'month') from dual
select date'2013-01-28'+numtodsinterval(31,'day') from dual
3.日期差值
直接相减
select date'2013-01-31'-to_date('2013-01-01 12:00:00','yyyy-mm-dd hh24:mi:ss') from dual
months_between函数
select months_between(date'2013-01-31',to_date('2010-11-01 12','yyyy-mm-dd hh24')) from dual;
4.日期舍入
– trunc函数
select trunc(to_date('12:35:35','hh24:mi:ss'),'MI') from dual;
select trunc(to_date('12:35:35','hh24:mi:ss'),'HH') from dual;
– ROUND函数
select round(date'2013-04-15','MM') from dual;
select round(to_date('12:35:35','hh24:mi:ss'),'MI') from dual;
5.其他常规函数
NEXT_DAY,LAST_DAY