生成两个日期之间的日期

时间:2013-04-25 06:07:02

标签: sql oracle datetime

我需要生成两个给定日期之间的所有日期。只要只有一个日期范围,这样就可以正常工作。但是,如果我有多个日期范围,则此解决方案不起作用。我在这里以及在asktom上搜索过,但找不到相关的指针/解决方案。

我使用all_objects和CONNECT BY ROWNUM尝试了两种解决方案,但没有运气。以下是问题陈述:sqlfiddle

输入

ID  START_DATE      END_DATE 
101 April, 01 2013  April, 10 2013 
102   May, 10 2013    May, 12 2013

输出

ID  Dates
101 April, 01 2013 
101 April, 02 2013  
101 April, 03 2013  
101 April, 04 2013  
101 April, 05 2013  
101 April, 06 2013  
101 April, 07 2013  
101 April, 08 2013  
101 April, 09 2013  
101 April, 10 2013  
102   May, 10 2013  
102   May, 11 2013  
102   May, 12 2013

4 个答案:

答案 0 :(得分:6)

select 
  A.ID, 
  A.START_DATE+delta dt
from 
  t_dates A, 
  (
     select level-1 as delta 
     from dual 
     connect by level-1 <= (
       select max(end_date - start_date) from t_dates
     )
  )
where A.START_DATE+delta <= A.end_date
order by 1, 2

答案 1 :(得分:3)

请尝试:

select 
  distinct ID, 
  START_DATE+level-1 DATES
from dual a, TABLE_DATES b
connect by level <= (END_DATE-START_DATE)+1
order by ID;

答案 2 :(得分:1)

    select g.cycle_dt
        from 
            (select to_date(d,'DD-MM-YYYY') cycle_dt
               from dual
                    model
                    dimension by (trunc(to_date('30092015', 'DDMMYYYY')) d) 
                    measures (0 y)
                    rules (
                    y[for d from trunc(to_date('30092015', 'DDMMYYYY')) to to_date('30102015', 'DDMMYYYY')  increment 1]=0
                    )) g
        order by g.cycle_dt;

答案 3 :(得分:0)

WITH NUMS AS (
  SELECT LEVEL-1 DaysToAdd
  FROM DUAL
  CONNECT BY LEVEL <= (
    SELECT MAX(END_DATE - START_DATE + 1)
    FROM HOLIDAY)
)
SELECT START_DATE + DaysToAdd DATE_TIME, DESCRIPTION, IS_SCHOOL_HOLIDAY, UPDATE_BY, CREATE_DATE
FROM HOLIDAY
CROSS JOIN NUMS
WHERE HOLIDAY.END_DATE - HOLIDAY.START_DATE  > DaysToAdd
ORDER BY 1, 2;