oracle 日期大全

3,556 阅读1分钟

时间比较

时间 between 开始时间 and 结束时间

时间格式化

to_date(sysdate,'yyyy-mm-dd HH:mm:ss')
to_char(last_day(sysdate), ‘yyyy-mm-dd’)

获取时间

date

select sysdate  from dual; 

trunc函数

-- 当月第一天
select trunc(sysdate, ‘mm’) from dual
-- 当年第一天
select trunc(sysdate,‘yy’) from dual
-- 当前年月日
select trunc(sysdate,‘dd’) from dual	
-- 当年第一天	
select trunc(sysdate,‘yyyy’) from dual	
-- 当前星期的第一天 (也就是星期天)	
select trunc(sysdate,‘d’) from dual
-- 当前日期	
select trunc(sysdate) from dual	
-- 当前时间(准确到小时)	
select trunc(sysdate, ‘hh’) from dual	
-- 前一天的日期	
select TRUNC(SYSDATE - 1) from dual	
-- 前一个月的日期	
select add_months(trunc(sysdate),-1) from dual
-- 后一个月的日期	
select add_months(trunc(sysdate),1) from dual 

char

-- 时间格式化
select to_char(sysdate,‘yyyy-MM-dd HH:mm:ss’) from dual	
-- 当前时间(准确到分钟)	
select to_char(trunc(sysdate, ‘mi’),‘yyyy-MM-dd HH:mm:ss’) from dual	
-- 本月最后一天	
select to_char(last_day(sysdate), ‘yyyy-mm-dd’) from dual