Hi All,
I need to find last date of month for given year.
Example:
I used to pass date or year such as 2012 or 01-01-2012(DD-MM-YYYY)
SQL query needs to return last date of every month such as
31-01-2012
28-02-2012
31-03-2012
30-04-2012
.
.
.
.
.
31-12-2012
for above requirement i have written the following SQL
select rownum as row_count,
case when rownum=1 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as Jan_month,
case when rownum=2 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as Feb_month,
case when rownum=3 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as mar_month,
case when rownum=4 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as apr_month,
case when rownum=5 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as may_month,
case when rownum=6 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as jun_month,
case when rownum=7 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as jul_month,
case when rownum=8 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as aug_month,
case when rownum=9 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as sep_month,
case when rownum=10 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as oct_month,
case when rownum=11 then last_day(to_date(add_months(trunc(to_date('01-01-2014','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as nov_month,
case when rownum=12 then last_day(to_date(add_months(trunc(to_date('01-01-2012','DD-MM-YYYY'), 'YYYY'), level - 1), 'DD-MM-YY')) end as dec_month
from dual connect by level <= 12 order by rownum;
Result
| Jan_Month | Feb_Month | Mar_Month | Apr_Month | may_month | jun_month | jul_month | aug_month | sep_month | oct_month | nov_month | dec_month |
1 | 31-01-2014 | | | | | | | | | | | |
2 | | 28-02-2014 | | | | | | | | | | |
3 | | | 31-03-2014 | | | | | | | | | |
4 | | | | 30-04-2014 | | | | | | | | |
5 | | | | | 31-05-2014 | | | | | | | |
6 | | | | | | 30-06-2014 | | | | | | |
7 | | | | | | | 31-07-2014 | | | | | |
8 | | | | | | | | 31-08-2014 | | | | |
9 | | | | | | | | | 30-09-2014 | | | |
10 | | | | | | | | | | 31-10-2014 | | |
11 | | | | | | | | | | | 30-11-2014 | |
12 | | | | | | | | | | | | 31-12-2012 |
Excepted Result:
am excepted result as single row such as
Jan_Month | Feb_Month | Mar_Month | Apr_Month | may_month | jun_month | jul_month | aug_month | sep_month | oct_month | nov_month | dec_month |
31-01-2012 | 28-02-2012 | 31-03-2012 | 30-04-2012 | 31-05-2012 | 30-06-2012 | 31-07-2012 | 31-08-2012 | 30-09-2012 | 31-10-2012 | 30-11-2012 | 31-12-2012 |
Kindly give me suggestion to archive above result.
Thanks&Regards
Sami