例如,请考虑这两个日期&#34; 2012-04-02&#34; &#34; 2012-04-30&#34; 我希望获得如下所示的输出,但不应包含此日期&#34; 2012-04-02&#34; &#34; 2012-04-30&#34; < /强>
**COLA**
2012-04-01
2012-04-03
2012-04-04
2012-04-05
2012-04-06
2012-04-07
..
..
..
2012-04-29
2012-04-31
答案 0 :(得分:0)
使用分层查询:
SELECT start_date + LEVEL - 1
FROM (
SELECT DATE '2012-04-02' + 1 AS start_date,
DATE '2012-04-30' - 1 AS end_date
FROM DUAL
)
CONNECT BY start_date + LEVEL - 1 <= end_date;
或者,递归子查询分解子句:
WITH dates ( value ) AS (
SELECT CAST( DATE '2014-04-02' + 1 AS DATE ) FROM DUAL
UNION ALL
SELECT value + 1 FROM dates WHERE value + 1 < DATE '2014-04-30'
)
SELECT * FROM dates;
或者,如果您在整个月内执行此操作,则除了这些日期:
WITH boundaries ( start_date, end_date ) AS (
SELECT DATE '2014-04-02', DATE '2014-04-30' FROM DUAL
)
SELECT TRUNC( start_date, 'MM' ) + LEVEL - 1
FROM boundaries
WHERE TRUNC( start_date, 'MM' ) + LEVEL - 1 NOT IN ( start_date, end_date )
CONNECT BY TRUNC( start_date, 'MM' ) + LEVEL - 1 <= LAST_DAY( end_date );
或:
WITH boundaries ( start_date, end_date ) AS (
SELECT DATE '2014-04-02', DATE '2014-04-30' FROM DUAL
),
dates ( value ) AS (
SELECT TRUNC( start_date, 'MM' ) FROM boundaries
UNION ALL
SELECT value + 1 FROM dates WHERE value + 1 <= LAST_DAY( value )
)
SELECT *
FROM dates d
WHERE NOT EXISTS ( SELECT 1 FROM boundaries WHERE d.value IN ( start_date, end_date ) );