Get a list of dates between two dates

后端 未结 20 2139
醉梦人生
醉梦人生 2020-11-22 00:26

Using standard mysql functions is there a way to write a query that will return a list of days between two dates.

eg given 2009-01-01 and 2009-01-13 it would return

20条回答
  •  一整个雨季
    2020-11-22 00:51

    For MSSQL you can use this. It is VERY quick.

    You can wrap this up in a table valued function or stored proc and parse in the start and end dates as variables.

    DECLARE @startDate DATETIME
    DECLARE @endDate DATETIME
    
    SET @startDate = '2011-01-01'
    SET @endDate = '2011-01-31';
    
    WITH dates(Date) AS 
    (
        SELECT @startdate as Date
        UNION ALL
        SELECT DATEADD(d,1,[Date])
        FROM dates 
        WHERE DATE < @enddate
    )
    
    SELECT Date
    FROM dates
    OPTION (MAXRECURSION 0)
    GO
    

提交回复
热议问题