Introduction: In this short article I am going to share how to get all dates between the date range we specify in Sql server using CTE (Common table expression).
In previous articles i explained How to Add or subtract days, weeks, months, quarters and years in/from date and Get first,l ast date and total days in year or month and Get created or modified date of tables, stored procedures, views and functions and Get age in years, months and days from date of birth and Select records between specified range in sql server
Description: While working with sql server database we often need to deal with dates. Few days back I need to get the dates between the date range I specify. There are many ways to get this done but here I have shared how to get this using CTE.
Implementation: Let’s write the query using CTE to select the dates between date range.
DECLARE @StartDate DATE='2017-01-01', @EndDate DATE='2017-01-10';
;WITH DateCTE(DateList) AS
SELECT @StartDate as Date
FROM DateCTE WHERE DateList < @EndDate
SELECT DateList FROM DateCTE OPTION (MAXRECURSION 0)
Result will be as shown in image above.
Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.