Search code examples
oracledate-arithmetic

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.


Solution

  • 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.