Get a list of dates between two dates using a function

前端 未结 21 1104
天涯浪人
天涯浪人 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条回答
  •  旧时难觅i
    2020-11-22 06:56

    DECLARE @MinDate DATETIME = '2012-09-23 00:02:00.000',
        @MaxDate DATETIME = '2012-09-25 00:00:00.000';
    
    SELECT  TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1) Dates = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate)
    FROM sys.all_objects a CROSS JOIN sys.all_objects b;
    

提交回复
热议问题