This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output for getting the list of years between given dates. Use recursive CTE to achieve this.
Input Date Range: 2010-01-01 to 2018-01-01
Expected Output:
1 2 3 4 5 6 7 8 9 10 11 |
Years ----------- 2010 2011 2012 2013 2014 2015 2016 2017 2018 |
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 |
DECLARE @Start_Date date = '20100101', @End_Date date = '20180101'; ;WITH cte(DT) AS ( SELECT DT = @Start_Date UNION ALL SELECT DATEADD(YEAR, 1, DT) FROM cte WHERE DATEADD(YEAR, 1, DT) <= @End_Date ) SELECT YEAR(DT) AS Years FROM cte ORDER BY DT OPTION (MAXRECURSION 3660); |
Please try the different solution for this puzzle and share it via comment...