版权声明:本文为博主原创文章,未经博主允许不得转载。
在做数据分析时,时常用到上日,月初,月末,季初,季末,年初,年末,上月等等统计日期的口径,今天特意整理下,方便后续使用
上日:
select DATE'2016-08-09'-1 from dual;
月初:
select trunc(DATE'2016-08-09','MM') from dual;
月末:
select last_day(DATE'2016-08-09') from dual;
上月初:
select add_months(trunc(DATE'2016-08-09','MM'),-1) from dual;
上月末:
select trunc(DATE'2016-08-09','MM')-1 from dual;
季初:
select trunc(DATE'2016-08-09','Q') from dual;
季末:
select last_day(add_months(trunc(DATE'2016-08-09','Q'),2)) from dual;
上季初:
select add_months(trunc(DATE'2016-06-09','Q'),-3) from dual;
上季末:
select trunc(DATE'2016-08-09','Q')-1 from dual;
年初:
select trunc(DATE'2016-08-09','YYYY') from dual;
上年末:
select trunc(DATE'2016-08-09','Y')-1 from dual;
上年同期:
select add_months(d1,-12) into datenew from dual;
上月同期:
select add_months(DATE'2016-08-09',-1) from dual;
本年年中:
select add_months(trunc(DATE'2016-08-09','Y'),6)-1 from dual;