In this article, we will learn how to find all the Sundays between two given dates in SQL Server using CTE(Common Table Expression).
Here is the code to find all the Sundays between two dates in SQL Server using CTE.
declare @DateFrom DateTime ='2019-01-01',
@DateTo DateTime = '2019-01-31'
;WITH CTE(dt)
AS
(
Select @DateFrom
Union All
Select DATEADD(d,1,dt)FROM CTE
Where dt<@DateTo
)
select 'Sunday' as 'Day Name',dt as 'Sunday Date' from CTE
where DATENAME(dw,dt)In('Sunday');
Day Name Sunday Date
------------- -----------------------
Sunday 2019-01-06 00:00:00.000
Sunday 2019-01-13 00:00:00.000
Sunday 2019-01-20 00:00:00.000
Sunday 2019-01-27 00:00:00.000
I hope this article will help you to understand how to find all the Sundays between two given dates in SQL server.
Share your valuable feedback and help us to improve. If you find anything incorrect, or you want to share more information about the topic discussed above. please post your comment at the bottom of this article. Thank you!
Comments