How to Count Weekends in Any Month Using SQL Server: A Step-by-Step Guide

Опубликовано: 13 Июль 2026
на канале: Analytics Hub
103
5

"In this SQL Server tutorial, we'll show you how to count the number of weekends (Saturdays and Sundays) in any given month using a simple SQL query. Whether you're a beginner or an experienced developer, this step-by-step guide will help you master date functions in SQL Server. We'll break down the query so you can easily adapt it to your own projects. Perfect for data analysts, developers, and anyone working with SQL databases. Watch now and learn how to make the most of your SQL Server skills!"


--You are working on an analytics solution for a client who wants to observe that the sales for
the current month only occurred during the weekdays.
It seems that the weekend sales must be deducted from the total sales.
Can you find the number of weekends in the current month before that?

declare @year int =2024; --specify the year you want to check
declare @month int =3; --specify the month you want to check
with dateseries AS(
select
cast(dateadd(day,n.number,datefromparts(@year,@month,1)) as date) as currentdate
from master..spt_values n
where n.type='P'
and n.number (lessthan) datediff(day, datefromparts(@year,@month,1), dateadd(month,1,datefromparts(@year,@month,1)))
)
select *
--count(*) as weekenddays
from dateseries
where datepart(weekday,currentdate) in (1,7)

SQL Interview Problem: Splitting Full Names into First, Middle, and Last Names
   • SQL Interview Problem: Splitting Full Name...  

Top SQL Interview Questions: RANK, DENSE_RANK, ROW_NUMBER Explained
   • Top SQL Interview Questions: RANK, DENSE_R...  

Top 3 Products by Sales & Employees by Salary | SQL Ranking Functions Explained
   • Top 3 Products by Sales & Employees by Sal...  

SQL Interview Challenge: Find Hidden Likes in Your Friends' Favorites!
   • SQL Interview Challenge: Find Hidden Likes...  

SQL Magic: Aggregating Marks & Delivery Dates with Advanced Queries!
   • SQL Magic: Aggregating Marks & Delivery Da...  

Top 20% vs Bottom 20% of Students in SQL: NTILE vs TOP PERCENT
   • Top 20% vs Bottom 20% of Students in SQL: ...  

SQL Tricks: Find the 3rd Lowest Salary in Each Department with Two Powerful Methods!
   • SQL Tricks: Find the 3rd Lowest Salary in ...  

SQL Tutorial: Find Employees Who Joined Before Their Managers
   • SQL Tutorial: Find Employees Who Joined Be...  

SQL Tutorial: Retrieve Specific Rows with OFFSET & ROW_NUMBER - Top 2 Methods Compared!
   • SQL Tutorial: Retrieve Specific Rows with ...  

#SQL Server
#Date Functions
#Weekend Calculation
#SQL Tutorial
#SQL Query
#SQL Server Tips
#Data Analysis
#Programming
#Database Management