Get Last date of every Month in a year

Advertisement
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
Advertisement

Replay

I agree with Marcus Pivot is the way to go about this... But on the other hand you almost solved it yourself.. just a max function was needed for your expected output:
select
max(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,
max(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,
max(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,
max(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,
max(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,
max(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,
max(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,
max(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,
max(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,
max(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,
max(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,
max(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;
AND to avoid hardcoding you can also modify your query as :
select
max(case  when rownum=1 then last_day(to_date(to_char(rownum),'MM')) end) as Jan_month,
max(case  when rownum=2 then last_day(to_date(to_char(rownum),'MM')) end) as Feb_month,
max(case  when rownum=3 then last_day(to_date(to_char(rownum),'MM')) end) as mar_month,
max(case  when rownum=4 then last_day(to_date(to_char(rownum),'MM')) end) as apr_month,
max(case  when rownum=5 then last_day(to_date(to_char(rownum),'MM')) end) as may_month,
max(case  when rownum=6 then last_day(to_date(to_char(rownum),'MM')) end) as jun_month,
max(case  when rownum=7 then last_day(to_date(to_char(rownum),'MM')) end) as jul_month,
max(case  when rownum=8 then last_day(to_date(to_char(rownum),'MM')) end) as aug_month,
max(case  when rownum=9 then last_day(to_date(to_char(rownum),'MM')) end) as sep_month,
max(case  when rownum=10 then last_day(to_date(to_char(rownum),'MM')) end) as oct_month,
max(case  when rownum=11 then last_day(to_date(to_char(rownum),'MM')) end) as nov_month,
max(case  when rownum=12 then last_day(to_date(to_char(rownum),'MM')) end) as dec_month
from dual connect by level <= 12 order by rownum;
Easy way: (without connect)
SELECT LAST_DAY (TO_DATE (ROWNUM, 'MM')) AS Jan_month,
       LAST_DAY (TO_DATE (ROWNUM + 1, 'MM')) AS Feb_month,
       LAST_DAY (TO_DATE (ROWNUM + 2, 'MM')) AS Mar_month,
       LAST_DAY (TO_DATE (ROWNUM + 3, 'MM')) AS Apr_month,
       LAST_DAY (TO_DATE (ROWNUM + 4, 'MM')) AS May_month,
       LAST_DAY (TO_DATE (ROWNUM + 5, 'MM')) AS Jun_month,
       LAST_DAY (TO_DATE (ROWNUM + 6, 'MM')) AS Jul_month,
       LAST_DAY (TO_DATE (ROWNUM + 7, 'MM')) AS Aug_month,
       LAST_DAY (TO_DATE (ROWNUM + 8, 'MM')) AS Sep_month,
       LAST_DAY (TO_DATE (ROWNUM + 9, 'MM')) AS Oct_month,
       LAST_DAY (TO_DATE (ROWNUM + 10, 'MM')) AS Nov_month,
       LAST_DAY (TO_DATE (ROWNUM + 11, 'MM')) AS Dec_month
  FROM DUAL
Cheers,
Manik.