使用oracle sql列出所有月份

时间:2011-01-03 07:58:14

标签: sql oracle

伙计们,有没有更好的方法列出除此之外的所有月份:

select to_char(add_months(to_date('01/01/1000', 'DD/MM/RRRR'), ind.l-1), 'MONTH') as month_descr,
       ind.l as month_ind
  from dual descr,
       (select l
          from (select level l 
                  from dual 
                connect by level <= 12
               )
       ) ind
order by 2;

答案:

SELECT to_char(add_months(SYSDATE, (LEVEL-1 )),'MONTH') as months 
  FROM dual 
CONNECT BY LEVEL <= 1

下面还有一个问题

此外,我想列出前两年,包括当年。我写了这个SQL查询。如果有更好的事情,请告诉我。

select extract(year from sysdate) - (level-1) as years 
  from dual 
connect by level <=3 
order by years

5 个答案:

答案 0 :(得分:11)

不是更好,但只是更清洁一点:

SQL>  select to_char(date '2000-12-01' + numtoyminterval(level,'month'),'MONTH') as month
  2     from dual
  3  connect by level <= 12
  4  /

MONTH
---------
JANUARY
FEBRUARY
MARCH
APRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER

12 rows selected.

此致 罗布。

答案 1 :(得分:7)

烨。

1:

SELECT * FROM WWV_FLOW_MONTHS_MONTH;

2:( UPD :)

WITH MONTH_COUNTER AS (
  SELECT LEVEL-1 AS ID 
  FROM DUAL 
  CONNECT BY LEVEL <= 12
) 
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('01/01/1000', 'DD/MM/RRRR'), ID),'MONTH') FROM MONTH_COUNTER;

答案 2 :(得分:4)

select to_char(add_months(trunc(sysdate, 'yyyy'), level - 1), 'MONTH') months
  from dual
connect by level <= 12;

返回:

MONTHS
--------------------
JANUARY
FEBRUARY
MARCH
APRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER

12 rows selected.

答案 3 :(得分:1)

SELECT to_char(to_date( level,'mm'), 'MONTH') Months FROM DUAL CONNECT BY LEVEL <=12;

此致 Prasant Sutaria

答案 4 :(得分:0)

 SELECT TO_CHAR(TO_DATE(rownum||'-'||rownum||'-'||'2013', 'DD-MM-YYYY'), 'Month') 
 FROM    all_objects 
 WHERE rownum < 13