Calculating Week days in MSSQL

Had a requirement to calculate the weekdays between given date range (No of Days – Weekends). Finally with the help of WWW, I manage to figure out a way to do it in MS SQL

DECLARE @START_DATE = '2012-09-17'	

DECLARE @WEEKDAYS INT
SELECT @WEEKDAYS = (
   (DATEDIFF(dd, @START_DATE, GETDATE()) + 1)
  -(DATEDIFF(wk, @START_DATE, GETDATE()) * 2)
  -(CASE WHEN DATENAME(dw, @START_DATE) = 'Sunday' THEN 1 ELSE 0 END)
  -(CASE WHEN DATENAME(dw, GETDATE()) = 'Saturday' THEN 1 ELSE 0 END))
Did you enjoy this article?
Signup today and receive free updates straight in your inbox. We will never share or sell your email address.

Leave a Reply