Get a list of dates between two dates using a function

前端 未结 21 1309
天涯浪人
天涯浪人 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 06:38

    Declare @date1 date = '2016-01-01'
                  ,@date2 date = '2016-03-31'
                  ,@date_index date
    Declare @calender table (D date)
    SET @date_index = @date1
    WHILE @date_index<=@date2
    BEGIN
    INSERT INTO @calender
    SELECT @date_index
    
    SET @date_index = dateadd(day,1,@date_index)
    
    IF @date_index>@date2
    Break
    ELSE
    Continue
    END
    

提交回复
热议问题