Get a list of dates between two dates using a function

前端 未结 21 1428
天涯浪人
天涯浪人 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:42

    -- ### Six of one half dozen of another. Another method assuming MsSql

    Declare @MonthStart    datetime   = convert(DateTime,'07/01/2016')
    Declare @MonthEnd      datetime   = convert(DateTime,'07/31/2016')
    Declare @DayCount_int       Int   = 0 
    Declare @WhileCount_int     Int   = 0
    
    set @DayCount_int = DATEDIFF(DAY, @MonthStart, @MonthEnd)
    select @WhileCount_int
    WHILE @WhileCount_int < @DayCount_int + 1
    BEGIN
       print convert(Varchar(24),DateAdd(day,@WhileCount_int,@MonthStart),101)
       SET @WhileCount_int = @WhileCount_int + 1;
    END;
    

提交回复
热议问题