Listing Weekends of Selected Years in SQL Server

Hello everyone,

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.

Listing Weekends of Selected Years in SQL Server

As you can see, we have listed the weekends of the selected years.

Good luck to everyone in business and life.

64 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!