Oracle 查询指定年份(比如2008)每个月的第一天和最后一天

Oracle 查询指定年份(比如2008)每个月的第一天和最后一天
2024-12-02 08:37:21
推荐回答(2个)
回答1:

查询2008年第一天:

select trunc(to_date('2008','yyyy'),'year') from dual;

结果:

查询2008年最后一天:

select trunc(to_date(to_char(2008+1),'yyyy'),'year')-1 from dual;

结果:

回答2:

select to_date(mon,'yyyymmdd'),add_months(to_date(mon,'yyyymmdd'),1)-1
from (
select '2008'||aa||'01' mon from (
select '01' aa from dual union
select '02' aa from dual union
select '03' aa from dual union
select '04' aa from dual union
select '05' aa from dual union
select '06' aa from dual union
select '07' aa from dual union
select '08' aa from dual union
select '09' aa from dual union
select '10' aa from dual union
select '11' aa from dual union
select '12' aa from dual));

下面的可能更好:
select to_date(mon,'yyyymmdd'),LAST_DAY(to_date(mon,'yyyymmdd'))
from (
select '2008'||to_char(aa,'09')||'01' mon from (
select 1 aa from dual union
select 2 aa from dual union
select 3 aa from dual union
select 4 aa from dual union
select 5 aa from dual union
select 6 aa from dual union
select 7 aa from dual union
select 8 aa from dual union
select 9 aa from dual union
select 10 aa from dual union
select 11 aa from dual union
select 12 aa from dual));