如何使用查询获取2个给定日期之间的月份列表? [英] How to get a list of months between 2 given dates using a query?

查看:166
本文介绍了如何使用查询获取2个给定日期之间的月份列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个约会,例如2011年3月28日和2011年6月29日.我需要一个SQL查询,该查询将显示这两个日期之间的月份,包括包含日期的月份,即.六月,五月,四月和三月.

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.

推荐答案

类似这样的东西

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

应该工作.

这篇关于如何使用查询获取2个给定日期之间的月份列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆