2014-09-29 154 views
2

我有這個表:插入一系列日期到SQL開始日期和結束日期輸入

**ID StartDate EndDate** 
1 01/01/2012 03/01/2012 
2 28/09/2013 02/10/2013 
3 12/06/2011 15/06/2011 

,我需要有這個表: 日期

**ID Date** 
1 01/01/2012 
1 02/01/2012 
1 03/01/2012 
2 28/09/2013  
2 29/09/2013 
2 30/09/2013 
2 01/10/2013 
2 02/10/2013 
3 12/06/2011 
3 13/06/2011 
3 14/06/2011  
3 15/06/2011 

我旁邊SQL代碼Retarn起始日期&之間的日期的結束日期+起始日期+結束日期:

declare @Start datetime 
declare @end datetime 
declare @request int 

set @Start = '2014-09-28 06:53:04.560' 
set @end = '2014-09-29 11:53:04.560' 
set @request = 1 

;with Dates as (
    select @request as reqId,@Start as reqDate 
    union all 
    select reqId+1,DATEADD(hh,1,reqDate) from Dates 
    where reqDate < @end 
) 
select * from Dates 

如何獲得此批結果的StartDate-EndDate輸入結果?

+0

使用日曆表可以讓您的查詢更加簡單。 http://blog.jontav.com/post/9380766884/calendar-tables-are-incredibly-useful-in-sql。 – mhn 2014-09-29 08:20:08

回答

1

您可以使用您的源日期表如下

declare @request int 

set @request = 1 

;with Dates as (
    SELECT @request as reqId,StartDate as reqDate, EndDate 
    FROM yourDateTable 
    UNION ALL 
    SELECT reqId+1,DATEADD(DAY,1,reqDate),Dates.EndDate 
    FROM Dates 
    WHERE DATEADD(DAY,1,reqDate) < EndDate 
) 
SELECT * 
FROM Dates 
+0

非常感謝!!!!!!!它工作! – Sara 2014-09-29 08:57:52

0

使用遞歸查詢

CREATE TABLE #ranges 
    (
     Id INT , 
     startDate DATE , 
     ENdDate DATE 
    ) 
INSERT #ranges 
     (Id, startDate, ENdDate) 
VALUES (1, '2014-5-2', '2014-5-5'), 
     (2, '2014-8-29', '2014-9-3'), 
     (3, '2014-10-2', '2014-10-8'); 
WITH 
     cte 
      AS (SELECT * 
       FROM  #ranges 
       UNION ALL 
       SELECT id , 
         DATEADD(DAY, 1, startDate) , 
         EndDate 
       FROM  cte 
       WHERE DATEADD(DAY, 1, startDate) <= EndDate 
      ) 
    SELECT Id , 
      startDate Date 
    FROM cte 
    ORDER BY Id , 
      startDate 


DROP TABLE #ranges 
0
DECLARE @MyPeriod TABLE (
    ID INT NOT NULL IDENTITY PRIMARY KEY 
    ,StartDate date 
    ,EndDate date 
); 
INSERT INTO @MyPeriod (StartDate, EndDate) 
VALUES ('20120101','20120103') 
,('20130928','20131002') 
,('20110612','20110615'); 

SELECT * 
FROM @MyPeriod 

declare @Start date 
declare @end date 

set @Start = '2000-01-01' 
set @end = '2020-01-01' 

;with Dates as (
    select @Start as Date 
    union all 
    select DATEADD(day,1,Date) from Dates 
    where Date < @end 
) 
select MyPeriod.ID, Dates.Date 
from Dates 
    INNER JOIN @MyPeriod MyPeriod 
     ON Dates.Date BETWEEN MyPeriod.StartDate AND MyPeriod.EndDate 
ORDER BY Dates.Date 
OPTION(MAXRECURSION 0); 
0

試試這個做到這一點!

CREATE TABLE #TempTable( 
    ID  int, 
    [Date] date 
) 

DECLARE @ROWCOUNT INT,@ID INT 
DECLARE @Date1 DATE, @Date2 DATE 
SET @ROWCOUNT = 1 
SET @ID='' 
SET @Date1='' 
SET @Date1='' 
WHILE (@ROWCOUNT <= (SELECT COUNT(*) FROM your_table_name)) 
    BEGIN 
     SET @ID=(SELECT id FROM your_table_name WHERE [email protected]) 
     SET @Date1=(SELECT StartDate FROM your_table_name WHERE [email protected]) 
     SET @Date2=(SELECT EndDate FROM your_table_name WHERE [email protected]) 

     INSERT INTO #TempTable 
     SELECT @ID, DATEADD(DAY,number,@Date1) [Date] 
     FROM master..spt_values 
     WHERE type = 'P' 
     AND DATEADD(DAY,number, @Date1) <= @Date2 

     SET @ROWCOUNT = @ROWCOUNT + 1 
    END 

SELECT * FROM #TempTable 
DROP TABLE #TempTable 
相關問題