Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get Last date of every Month in a year

samiJan 9 2014 — edited Jan 10 2014

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_MonthFeb_MonthMar_MonthApr_Monthmay_monthjun_monthjul_monthaug_monthsep_monthoct_monthnov_monthdec_month
131-01-2014
228-02-2014
331-03-2014
430-04-2014
531-05-2014
630-06-2014
731-07-2014
831-08-2014
930-09-2014
1031-10-2014
1130-11-2014
1231-12-2012

Excepted Result:

am excepted result as single row such as

Jan_MonthFeb_MonthMar_MonthApr_Monthmay_monthjun_monthjul_monthaug_monthsep_monthoct_monthnov_monthdec_month
31-01-201228-02-201231-03-201230-04-201231-05-201230-06-201231-07-201231-08-201230-09-201231-10-201230-11-201231-12-2012

Kindly give me suggestion to archive above result.

Thanks&Regards

Sami

This post has been answered by Manik on Jan 9 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 7 2014
Added on Jan 9 2014
14 comments
12,828 views