将一系列日期插入到sql startdate和enddate输入中

时间:2014-09-29 07:56:01

标签: sql sql-server date between

我有这张桌子:

**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代码,重新启动StartDate和amp;之间的日期EndDate + StartDate + EndDate:

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输入获取此结果?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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);

答案 3 :(得分: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 ID=@ROWCOUNT)
        SET @Date1=(SELECT StartDate FROM your_table_name WHERE ID=@ROWCOUNT)
        SET @Date2=(SELECT EndDate FROM your_table_name WHERE ID=@ROWCOUNT)

        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