列出基于当前日期的所有日期

时间:2014-04-03 16:56:05

标签: sql oracle

我要求做这样的事情, 如果我选择当前的月份日期,它应该给我结果(比如说今天)

 1. 04/01/2014        04/01/2014
 2. 04/02/2014        04/01/2014
 3. 04/02/2014        04/02/2014
 4. 04/03/2014        04/01/2014
 5. 04/03/2014        04/02/2014
 6. 04/03/2014        04/03/2014

等等任何日期,比如下一个日期,它应该上升到4月4日。在Oracle中有没有办法做到这一点。 请帮帮我。 感谢

1 个答案:

答案 0 :(得分:0)

使用此查询:

WITH dates AS (SELECT to_date('04-APR-2014', 'DD-MON-YYYY') ddate -- Your Date here
                 FROM dual),
dates1 AS (SELECT trunc(ddate, 'MM') + LEVEL - 1 ddate1
             FROM dates
          CONNECT BY trunc(ddate, 'MM') + LEVEL - 1 <= trunc(ddate))
SELECT ddate1, trunc(ddate1, 'MM') + LEVEL -1
  FROM dates1
 CONNECT BY trunc(ddate1, 'MM') + LEVEL -1 <= ddate1
   AND PRIOR ddate1 = ddate1
   AND PRIOR sys_guid() IS NOT NULL;

输出:

DDATE1  TRUNC(DDATE1,'MM')+LEVEL-1
4/1/2014    4/1/2014
4/2/2014    4/1/2014
4/2/2014    4/2/2014
4/3/2014    4/1/2014
4/3/2014    4/2/2014
4/3/2014    4/3/2014
4/4/2014    4/1/2014
4/4/2014    4/2/2014
4/4/2014    4/3/2014
4/4/2014    4/4/2014