如何使用查询获取2个给定日期之间的月份列表?

时间:2011-08-03 13:07:42

标签: oracle date-arithmetic

我有2个日期,比如2011年3月28日和2011年6月29日。我需要一个sql查询,它将显示这两个日期之间的月份,包括包含日期的月份,即。六月,五月,四月和三月。

6 个答案:

答案 0 :(得分:17)

像这样的东西

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

应该有用。

答案 1 :(得分:1)

您可以使用MONTHS_BETWEEN

功能
SELECT MOD( TRUNC( MONTHS_BETWEEN( '2011-07-29', '2011-03-28' ) ), 12 ) as MONTHS
FROM DUAL

输出

    MONTHS
----------
         4

答案 2 :(得分:1)

几天前我需要回答这个问题。我找到了另一种我更喜欢的解决方案:

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
)

您当然可以使用任何您想要的格式。我'联合'并总结了另一套,以便即使他们没有结果我也能得到几个月。

答案 3 :(得分:1)

要添加此解决方案只是因为我认为它比其他解决方案更清晰:

SELECT ADD_MONTHS(TRUNC(TO_DATE('28-Mar-2011', 'DD-MON-YYYY'), 'MON'), ROWNUM - 1) date_out
FROM   DUAL
CONNECT BY ADD_MONTHS(TRUNC(TO_DATE('28-Mar-2011', 'DD-MON-YYYY'), 'MON'), ROWNUM - 1)
    <= TRUNC(TO_DATE('29-Jun-2011', 'DD-MON-YYYY'), 'MON')

答案 4 :(得分:0)

SELECT MIN (to_date((TO_CHAR (Actual_Date, 'DD-MM-RRRR')),'dd-mm-rrrr')) F_DATE,
         MAX (to_date((TO_CHAR (Actual_Date, 'DD-MM-RRRR')),'dd-mm-rrrr')) T_DATE,
         TO_CHAR (Actual_Date, 'MM-RRRR') TRX_MONTH     
    FROM (    SELECT TRUNC (TO_DATE (:P_FDATE, 'dd-mm-rrrr')) + LEVEL - 1
                        Actual_Date
                FROM (SELECT TRUNC (TO_DATE (:P_FDATE, 'dd-mm-rrrr'), 'MM') - 1
                                AS dt
                        FROM DUAL)
          CONNECT BY LEVEL <=
                        (  TO_DATE (:P_TDATE, 'dd-mm-rrrr')
                         - TRUNC (TO_DATE (:P_FDATE, 'dd-mm-rrrr'))
                         + 1))
GROUP BY TO_CHAR (Actual_Date, 'MM-RRRR')
ORDER BY 1

答案 5 :(得分:0)

     declare 
        v_date_from_first_day date;
        v_date_to_last_day date;
        v_month_name varchar2(10);
        v_month_number number;
        v_year_number number;
        v_month_diff number;
        begin
          v_date_to_last_day := to_date('31.12.2018'); 
          v_date_from_first_day := to_date('01.01.2018');

          select months_between(v_date_to_last_day,v_date_from_first_day) as diff into v_month_diff from dual;

          for i in 1..round(v_month_diff, 2) loop
          select 
          to_char(trunc(add_months(v_date_to_last_day - months_between(v_date_from_first_day, v_date_to_last_day), -i)), 'fmMonth') as month_nm,
          to_char(trunc(add_months(v_date_to_last_day - months_between(v_date_from_first_day, v_date_to_last_day), -i)), 'MM') as month_num,
          to_char(trunc(add_months(v_date_to_last_day - months_between(v_date_from_first_day, v_date_to_last_day), -i)), 'YYYY') as year_num
          into v_month_name, v_month_number, v_year_number
          from dual;

          dbms_output.put_line(v_month_number || '/' || v_year_number);
dbms_output.put_line(v_month_name || '/' || v_year_number);
          end loop;
        end;

        Output: 
        12/2018
        11/2018
        10/2018
        9/2018
        8/2018
        7/2018
        6/2018
        5/2018
        4/2018
        3/2018
        2/2018
        1/2018

在这里,月份名称用克罗地亚语

Prosinac/2018
Studeni/2018
Listopad/2018
Rujan/2018
Kolovoz/2018
Srpanj/2018
Lipanj/2018
Svibanj/2018
Travanj/2018
Ožujak/2018
Veljača/2018
Siječanj/2018