Something like that: Oracle 11 and higher (maybe Oracle 9 and 10 too...)
If Your week begins with Sunday, maybe You should subtract 1 day in the inner sql.
("select trunc(dt, 'IW')-1 as beg_period" for week beginning with Sunday -instead of "select trunc(dt, 'IW') as beg_period" for week beginning with Monday)
with periods as
(
select greatest(to_date(&date_from, 'dd.mm.yyyy'), beg_period) beg_period -- date_from or first day of week
, least(to_date(&date_to, 'dd.mm.yyyy'), lead(beg_period) over (order by beg_period)-1 ) end_period -- date_to or last day of week (and =NULL if week begins AFTER date_to)
from
(select trunc(dt, 'IW')-1 as beg_period -- calc the beginning of week for every day generated, taking week beginning in Sunday ("-1")
from
(-- generate all the days between &date_from and FIRST DAY OF NEXT WEEK JUST AFTER &date_to (including)
select to_date(&date_from, 'dd.mm.yyyy') + (level-1) as dt
from dual
connect by level <= (trunc(to_date(&date_to, 'dd.mm.yyyy'), 'IW') + 7 -- also calc the first day of next week just AFTER date_to; it needed
-- for lead(...) in further (just to calc end-of-week for date_to, by lead(...))
- to_date(&date_from, 'dd.mm.yyyy')
+ 1
)
)
group by trunc(dt, 'IW') -- take only 1 beg_period for every week (may use "group by", or "distinct")
)
)
select beg_period
, end_period - case to_char(end_period, 'DAY','NLS_DATE_LANGUAGE=''numeric date language''') when '7' then 2 when '6' then 1 else 0 end
as end_period -- correction for weekend (Friday and Saturday) for week beginning in Sunday
from periods
where end_period is not null -- exclude next week just after date_to (which has end_period=NULL)
order by 1
;
See the last "case": it depends on NLS. In my expression, I mean Saturday='7' and Friday='6' (I guess it's correct if week begins with Sunday).