Get a list of dates between two dates using a function

前端 未结 21 1105
天涯浪人
天涯浪人 2020-11-22 06:25

My question is similar to this MySQL question, but intended for SQL Server:

Is there a function or a query that will return a list of days between two dates? For exa

21条回答
  •  太阳男子
    2020-11-22 07:02

    DECLARE @StartDate DATE = '2017-09-13',         @EndDate DATE = '2017-09-16'
    
    SELECT date  FROM (   SELECT DATE = DATEADD(DAY, rn - 1, @StartDate)   FROM    (
        SELECT TOP (DATEDIFF(DAY, @StartDate, DATEADD(DAY,1,@EndDate)))
          rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
        FROM sys.all_objects AS s1
        CROSS JOIN sys.all_objects AS s2
        ORDER BY s1.[object_id]   ) AS x ) AS y
    

    Result:

    2017-09-13
    
    2017-09-14
    
    2017-09-15
    
    2017-09-16
    

提交回复
热议问题