记录常用时间函数以及处理时间的sql语句
(1)获得系统时间
select sysdate from dual;
(2)设置时间的格式
select to_char(sysdate,'yyyy/MM/dd') ,to_char(sysdate,'yyyy-MM-dd HH:mm:ss') from dual;
(3)两个时间比较相差的天,时,分···
select sysdate,ROUND(TO_NUMBER(sysdate - to_date('2017-2-20 15:35:34','yyyy-mm-dd hh24:mi:ss'))) days from dual;
两个Date类型字段:START_DATE,END_DATE,计算这两个日期的时间差(分别以天,小时,分钟,秒,毫秒):
天:
ROUND(TO_NUMBER(END_DATE - START_DATE))
小时:
ROUND(TO_NUMBER(END_DATE - START_DATE) * 24)
分钟:
ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60)
秒:
ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60 * 60)
毫秒:
ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60 * 60 * 1000)
(4)获取当前日期中的年,月,日
select extract(year from sysdate) as "year" from dual;
select extract(month from sysdate) as "month" from dual;
select extract(day from sysdate) as "day" from dual;
(5)返回时区
select dbtimezone from dual;
(6)返回某个日期的下周的某一天
select next_day(to_date('20170222','YYYYMMDD'),2) from dual;
ps:1=星期天 2=星期一 3=星期二 4=星期三 5=星期四 6=星期五 7=星期六
(7)当前日期往后退多少个月
select sysdate, add_months(sysdate,2) from dual;
(8)本月最后一天
select last_day(sysdate) from dual;
(9)会话日期
select localtimestamp from dual;