Listing Weekends of Selected Years in SQL Server
In this article, I will try to give information about listing the weekends of selected years in SQL Server.
In SQL Server, in some cases you may want to list the weekends of selected years.
You can easily do this using the code below.
DECLARE @startDate DATETIME, @endDate DATETIME SELECT @startDate = '2023-01-01', @endDate = '2023-12-31' ;WITH Calender AS ( SELECT @startDate AS dt UNION ALL SELECT dt + 1 FROM Calender WHERE dt + 1 <= @endDate ) SELECT dt ,NameMonth = DATENAME(Month, dt) ,NameDay = DATENAME (Weekday,dt) ,WeekofYr = DATEPART(WEEK, dt) FROM Calender WHERE DATENAME (Weekday,dt) IN ('Sunday','Saturday') Option(MaxRecursion 0)
When you create and run the above code, you will see a result similar to the one below.
As you can see, we have listed the weekends of the selected years.
Good luck to everyone in business and life.