Get a list of dates between two dates using a function

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

    this few lines are the simple answer for this question in sql server.

    WITH mycte AS
    (
      SELECT CAST('2011-01-01' AS DATETIME) DateValue
      UNION ALL
      SELECT  DateValue + 1
      FROM    mycte   
      WHERE   DateValue + 1 < '2021-12-31'
    )
    
    SELECT  DateValue
    FROM    mycte
    OPTION (MAXRECURSION 0)
    

提交回复
热议问题