oracle中两个表之间的日期列表

时间:2016-02-11 07:53:57

标签: sql oracle date-arithmetic

问题

输入:

START_DATE      END_DATE 

01-FEB-16       03-FEB-16   
01-FEB-16       02-FEB-16 
10-FEB-16       11-FEB-16     

我想生成start_day和end_day之间的所有日期

Output

01-FEB-16    
02-FEB-16     
03-FEB-16     
10-FEB-16     
11-FEB-16      

1 个答案:

答案 0 :(得分:3)

您可以使用 Row Generator 技术来完成。

<强>设置

SQL> CREATE TABLE t
  2    (START_DATE DATE, END_DATE DATE
  3    );

Table created.

SQL> INSERT INTO t VALUES(DATE '2016-02-01', DATE '2016-02-03');

1 row created.

SQL> INSERT INTO t VALUES(DATE '2016-02-01', DATE '2016-02-02');

1 row created.

SQL> INSERT INTO t VALUES(DATE '2016-02-10', DATE '2016-02-11');

1 row created.

SQL> COMMIT;

Commit complete.

<强>查询

SQL> SELECT DISTINCT TO_CHAR(START_DATE+LEVEL-1, 'DD-MON-YYYY') the_date
  2  FROM t
  3    CONNECT BY LEVEL <= END_DATE-START_DATE+1
  4  ORDER BY the_date
  5  /

THE_DATE
-----------
01-FEB-2016
02-FEB-2016
03-FEB-2016
10-FEB-2016
11-FEB-2016

SQL>