Displaying all dates between two dates.
Table "tab_dates" contains the below data
Required Output:
DAY_DATE START_DATE END_DATE
------------------------- ------------------------- -------------------------
01-JAN-12 01-JAN-12 07-JAN-12
02-JAN-12 01-JAN-12 07-JAN-12
03-JAN-12 01-JAN-12 07-JAN-12
04-JAN-12 01-JAN-12 07-JAN-12
05-JAN-12 01-JAN-12 07-JAN-12
06-JAN-12 01-JAN-12 07-JAN-12
07-JAN-12 01-JAN-12 07-JAN-12
08-JAN-12 08-JAN-12 16-JAN-12
09-JAN-12 08-JAN-12 16-JAN-12
10-JAN-12 08-JAN-12 16-JAN-12
11-JAN-12 08-JAN-12 16-JAN-12
12-JAN-12 08-JAN-12 16-JAN-12
13-JAN-12 08-JAN-12 16-JAN-12
14-JAN-12 08-JAN-12 16-JAN-12
15-JAN-12 08-JAN-12 16-JAN-12
16-JAN-12 08-JAN-12 16-JAN-12
{code} -- Applies only to Oracle 11g database
with tab_dates as
(select to_date('01-JAN-12','dd-mon-yy') start_date,to_date('07-JAN-12','dd-mon-yy') end_date from dual
union all
select to_date('08-JAN-12','dd-mon-yy') start_date,to_date('14-JAN-12','dd-mon-yy') end_date from dual)
select start_date + i day_date, start_date, end_date from tab_dates,
xmltable('for $i in 0 to xs:int(D)-1 return $i' passing
xmlelement(D, (end_date-start_date)+1 ) columns i integer path '.') ;
{/code}
Explanation:
Using XMLTable() generate the list of numbers (ex: 0..6 for dates 6 days apart)
(end_date-start_date)+1 = number of days in-between and the result is substituted in the place of "D" using the XMLElement() function.
The temporary XMLTable now contains a column named "i".
Table "tab_dates" contains the below data
START_DATE | END_DATE
---------------------------------
01-JAN-12 07-JAN-12
08-JAN-12 16-JAN-12 01-JAN-12 07-JAN-12
Required Output:
DAY_DATE START_DATE END_DATE
------------------------- ------------------------- -------------------------
01-JAN-12 01-JAN-12 07-JAN-12
02-JAN-12 01-JAN-12 07-JAN-12
03-JAN-12 01-JAN-12 07-JAN-12
04-JAN-12 01-JAN-12 07-JAN-12
05-JAN-12 01-JAN-12 07-JAN-12
06-JAN-12 01-JAN-12 07-JAN-12
07-JAN-12 01-JAN-12 07-JAN-12
08-JAN-12 08-JAN-12 16-JAN-12
09-JAN-12 08-JAN-12 16-JAN-12
10-JAN-12 08-JAN-12 16-JAN-12
11-JAN-12 08-JAN-12 16-JAN-12
12-JAN-12 08-JAN-12 16-JAN-12
13-JAN-12 08-JAN-12 16-JAN-12
14-JAN-12 08-JAN-12 16-JAN-12
15-JAN-12 08-JAN-12 16-JAN-12
16-JAN-12 08-JAN-12 16-JAN-12
{code} -- Applies only to Oracle 11g database
with tab_dates as
(select to_date('01-JAN-12','dd-mon-yy') start_date,to_date('07-JAN-12','dd-mon-yy') end_date from dual
union all
select to_date('08-JAN-12','dd-mon-yy') start_date,to_date('14-JAN-12','dd-mon-yy') end_date from dual)
select start_date + i day_date, start_date, end_date from tab_dates,
xmltable('for $i in 0 to xs:int(D)-1 return $i' passing
xmlelement(D, (end_date-start_date)+1 ) columns i integer path '.') ;
{/code}
Explanation:
Using XMLTable() generate the list of numbers (ex: 0..6 for dates 6 days apart)
(end_date-start_date)+1 = number of days in-between and the result is substituted in the place of "D" using the XMLElement() function.
The temporary XMLTable now contains a column named "i".
No comments:
Post a Comment