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

后端 未结 6 2367
不思量自难忘°
不思量自难忘° 2020-12-10 15:19

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,

6条回答
  •  眼角桃花
    2020-12-10 15:28

    I needed an answer to this a couple of days ago. I found another solution I liked more:

    select to_char(which_month, 'Mon-yyyy') month
    from
    (
        select
            add_months(to_date(:start_date,'mm-yyyy'), rownum-1) which_month
        from
            all_objects
        where
            rownum <= months_between(to_date(:end_date,'mm-yyyy'), add_months(to_date(:start_date,'mm-yyyy'), -1))
        order by
            which_month
    )
    

    You could of course use any format you want. I 'union'ed and summed over another set so that I'd get the months even when they didn't have results.

提交回复
热议问题