Oracle获取日期&天数

一.获取当年所有日期

--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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值