一.获取当年所有日期
--select * from (
select trunc(sysdate,'yyyy')+rownum-1 as pdays
from dual connect by rownum<=add_months(trunc(sysdate,'yyyy'),12)-trunc(sysdate,'yyyy')
--)where pdays<trunc(sysdate)/*如此可查询从当年1号到昨天的日期*/
--去年所有日期
select add_months(trunc(sysdate,'yy'),-12)+rownum-1 as pdays
from dual connect by rownum<=trunc(sysdate,'yyyy')-add_months(trunc(sysdate,'yy'),-12)
--明年所有日期
select add_months(trunc(sysdate,'yy'),12)+rownum-1 as pdays
from dual connect by rownum<=add_months(trunc(sysdate,'yy'),24)-add_months(trunc(sysdate,'yy'),12)
--前年,去年,今年的所有日期
select add_months(trunc(sysdate,'yy'),-24)+rownum-1 as pdays
from dual connect by rownum<=add_months(trunc(sysdate,'yy'),12)-add_months(trunc(sysdate,'yy'),-24)
--用年份来获取一年
-- 把2023替换为用户传过来的数值
where pdays>=trunc(to_date('2023/01/01','yyyy/mm/dd','yy')
and pdays<add_months(to_date('2023/01/01','yyyy/mm/dd',12)
自定义
--前年,去年,今年、明年的所有日期
select add_months(trunc(sysdate,'yy'),-24)+rownum-1 as pdays from dual connect by rownum<=add_months(trunc(sysdate,'yy'),24)-add_months(trunc(sysdate,'yy'),-24)
--自定义讲解
select add_months(trunc(sysdate,'yy'),-24)/*开始日期*/+rownum-1 as pdays
from dual connect by rownum<=add_months(trunc(sysdate,'yy'),12)/*结束日期*/-add_months(trunc(sysdate,'yy'),-24)/*开始日期*/
二.获取当月所有日期
select trunc(sysdate,'mm')+rownum-1 as pdays
from dual connect by rownum<=to_number(trunc(to_char(last_day(sysdate),'dd')))
三.获取当年所有月份
select to_char(pday,'mm')pdays from
(select trunc(sysdate,'yyyy')+rownum-1 as pday
from dual connect by rownum<=add_months(trunc(sysdate,'yyyy'),12/*替换此数可动态调整查询的月份*/)-trunc(sysdate,'yyyy'))
group by to_char(pday,'mm')order by pdays
四.当月一号到昨天日期
select * from
(select to_char(sysdate-rownum+1,'yyyy/mm/DD') pdays from dual connect by rownum<=31)
where pdays BETWEEN (select to_char(trunc(sysdate,'mm'),'yyyy/mm/dd') from dual)
and to_char(trunc(sysdate)-1,'yyyy/mm/DD') order by pdays
五.今年月份&天数
select to_char(pday,'yyyy/mm')as pmonth,sum(num)num from
(select 1 num,trunc(sysdate,'yyyy')+rownum-1 as pday
from dual connect by rownum<=add_months(trunc(sysdate,'yyyy'),12)-trunc(sysdate,'yyyy'))
--where pday<trunc(sysdate,'mm') /*此处可限制具体月份*/
group by to_char(pday,'yyyy/mm') order by to_char(pday,'yyyy/mm')
六.某月天数
select add_months(to_date(to_char(sysdate/*可替换为想要查询的月份*/,'yyyy/mm'),'yyyy/mm'),1)
-to_date(to_char(sysdate/*可替换为想要查询的月份,于前参数一致*/,'yyyy/mm'),'yyyy/mm')m_num
from dual
七.当年天数
select add_months(trunc(sysdate,'yyyy'),12)-trunc(sysdate,'yyyy') y_num
from dual
八.当月天数
select to_number(to_char(last_day(trunc(sysdate)),'dd'))m_num
from dual
九.当年周数
select pw from(select to_char(pdays,'iyyy-iw')pw from(select trunc(sysdate,'yyyy')+rownum-1 as pdays
from dual connect by rownum<=add_months(trunc(sysdate,'yyyy'),12)-trunc(sysdate,'yyyy')))group by pw order by pw
十.当前第几周,某个日期为第几周
select to_char(sysdate,'iyyy-iw')as pw from dual
select to_char(date '2026-08-20','iyyy-iw')as pw from dual