with t as (
select to_date('03-05-2010','dd-mm-yyyy') d1,
to_date('26-08-2010','dd-mm-yyyy') d2
from dual
)
--
select decode(level,1,d1,trunc(add_months(d1,level-1),'mm')) as date_from,
case when add_months(trunc(d1,'mm'),level)>d2 then d2
else last_day(add_months(d1,level-1))
end date_to
from t
connect by add_months(trunc(d1,'mm'),level-1) < d2;
DATE_FROM DATE_TO
----------- -----------
03.05.2010 31.05.2010
01.06.2010 30.06.2010
01.07.2010 31.07.2010
01.08.2010 26.08.2010
Если хотим, чтобы в период входил 1 день последнего месяца, то :
with t as (
select to_date('03-05-2010','dd-mm-yyyy') d1,
to_date('01-08-2010','dd-mm-yyyy') d2
from dual
)
--
select decode(level,1,d1,trunc(add_months(d1,level-1),'mm')) as date_from,
case when add_months(trunc(d1,'mm'),level)>d2 then d2
else last_day(add_months(d1,level-1))
end date_to
from t
connect by add_months(trunc(d1,'mm'),level-1) <= d2;
DATE_FROM DATE_TO
----------- -----------
03.05.2010 31.05.2010
01.06.2010 30.06.2010
01.07.2010 31.07.2010
01.08.2010 01.08.2010
Последние комментарии