Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of months between 2 given dates using a query?

I have 2 dates, say 28-Mar-2011 and 29-Jun-2011. I need an sql query that will display the months between these 2 dates including the months containing the dates, ie. June, May, April and March.

like image 966
Nitish Avatar asked Aug 03 '11 13:08

Nitish


1 Answers

Something like this

SQL> ed
Wrote file afiedt.buf

    select to_char( add_months( start_date, level-1 ), 'fmMonth' )
      from (select date '2011-03-30' start_date,
                   date '2011-06-29' end_date
              from dual)
     connect by level <= months_between(
                           trunc(end_date,'MM'),
                           trunc(start_date,'MM') )
  *                      + 1
SQL> /

TO_CHAR(ADD_MONTHS(START_DATE,LEVEL-
------------------------------------
March
April
May
June

should work.

like image 59
Justin Cave Avatar answered Oct 29 '22 22:10

Justin Cave