SQL Server: How to Get the Number of Working Days in a Month

In this post, I want to show you how to count the weekdays in a month (defining the number of working days as being the days Monday to Friday)

In this below sample we're trying to get the number of working days in the current Month by inputting the current date. (GETDATE()). You can change the date according to your own needs.

DECLARE @Date DATETIME
SET @Date = GETDATE()
SELECT  20 + COUNT(*) AS WeekDayCount
FROM (
       SELECT DATEADD(MONTHDATEDIFF(MONTH, 0, @Date), 28) AS dDate UNION
       SELECT DATEADD(MONTHDATEDIFF(MONTH, 0, @Date), 29) UNION
       SELECT DATEADD(MONTHDATEDIFF(MONTH, 0, @Date), 30)
      ) AS d
WHERE  DATEPART(DAY, dDate) > 28
       AND DATEDIFF(DAY, 0, dDate) % 7 < 5


The date when I tried this query is June 17, 2022. So the query results will be as follows:


Post a Comment

0 Comments