Get a list of dates between two dates using a function

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

    In case you want to print years starting from a particular year till current date. Just altered the accepted answer.

    WITH mycte AS
        (
          SELECT YEAR(CONVERT(DATE, '2006-01-01',102)) DateValue
          UNION ALL
          SELECT  DateValue + 1
          FROM    mycte   
          WHERE   DateValue + 1 < = YEAR(GETDATE())
        )
        SELECT  DateValue
        FROM    mycte
    
    OPTION (MAXRECURSION 0)
    

提交回复
热议问题