Oracle数据库之日期查询

近期项目需求,需要查询不同情况下的时间点,整理了些,希望能对大家有所帮助

1、系统自带日期;

2、参考类型日期;

3、统一格式为字符串,可根据需求自由延伸;

--当前
select to_char(sysdate,'yyyyMMdd') from dual;
select to_char(to_date('20220914','yyyy/MM/dd'),'yyyyMMdd') from dual;

--上一日
select to_char(sysdate-1,'yyyyMMdd') from dual;
select to_char(to_date('20220914','yyyy/MM/dd')-1,'yyyyMMdd') from dual;

--近七天
select to_char(sysdate-7,'yyyyMMdd') from dual;
select to_char(to_date('20220914','yyyy/MM/dd')-7,'yyyyMMdd') from dual;

--当前月末
select to_char(last_day(sysdate),'yyyy/MM/dd') from dual;
select to_char(last_day(to_date('20220914','yyyy/MM/dd')),'yyyyMMdd') from dual;

--当前月初
select to_char(trunc(add_months(sysdate,0),'mm'),'yyyyMMdd') from dual;
select to_char(trunc(add_months(to_date('20220914','yyyy-MM-dd'),0),'mm'),'yyyyMMdd') from dual;

--上月末
select to_char(last_day(add_months(sysdate,-1)),'yyyyMMdd') from dual;
select to_char(last_day(add_months(to_date('20220914','yyyyMMdd'),-1)),'yyyyMMdd') from dual;

--上一年
select to_char(to_date(trunc(sysdate,'year') - 1),'yyyyMMdd') from dual;
select to_char(to_date(trunc(to_date('20220914','yyyyMMdd'),'year') - 1),'yyyyMMdd') from dual;

你可能感兴趣的:(数据库,数据库,oracle,sql)