Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / ATL
Technical Blog

How to Count Work Days Between 2 Dates in SQL Server

Rate me:
Please Sign up or sign in to vote.
4.60/5 (6 votes)
9 Jan 2014CPOL 81.7K   9   6
How to count work days between 2 dates in SQL Server

Introduction

This is a real quick post to explain how you can calculate the number of work days between 2 dates in SQL Server. This post considers the Workdays from Monday to Friday. In case you want the workdays from Monday to Saturday, read on. So, let us write a function that will display the number of work days between 2 dates in SQL Server.

SQL
CREATE FUNCTION [dbo].[CalculateNumberOFWorkDays] (@StartDate datetime, @EndDate datetime)
RETURNS int
AS
BEGIN
     
     SET @StartDate = DATEADD(dd, DATEDIFF(dd, 0, @StartDate), 0) 
     SET @EndDate = DATEADD(dd, DATEDIFF(dd, 0, @EndDate), 0) 
          
     DECLARE @WORKDAYS INT
     SELECT @WORKDAYS = (DATEDIFF(dd, @StartDate, @EndDate) + 1)
	               -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
   		       -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
		       -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)
  
     RETURN @WORKDAYS
END

In case you want the workdays to be calculated from Monday to Saturday, use the function below instead:

SQL
CREATE FUNCTION [dbo].[CalculateNumberOFWorkDays] (@StartDate datetime, @EndDate datetime)
RETURNS int
AS
BEGIN
     
     SET @StartDate = DATEADD(dd, DATEDIFF(dd, 0, @StartDate), 0) 
     SET @EndDate = DATEADD(dd, DATEDIFF(dd, 0, @EndDate), 0) 
          
     DECLARE @WORKDAYS INT
     SELECT @WORKDAYS = (DATEDIFF(dd, @StartDate, @EndDate) + 1)
	               -(DATEDIFF(wk, @StartDate, @EndDate))
   		       -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
  
     RETURN @WORKDAYS
END

Hope you like this post. In case you know any other method of calculating the work days, let us know via comments. Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

Love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks. #SOreadytohelp

Comments and Discussions

 
SuggestionThis might be helpful if Weekends are Friday and Saturday Pin
Zafar Raza27-Apr-17 1:56
Zafar Raza27-Apr-17 1:56 
QuestionHow to use this Function Pin
Member 117417784-Jun-15 3:32
Member 117417784-Jun-15 3:32 
QuestionFor official holiday Pin
sankarsan parida9-Jan-14 22:59
professionalsankarsan parida9-Jan-14 22:59 
AnswerRe: For official holiday Pin
Nitesh Kejriwal10-Jan-14 0:49
professionalNitesh Kejriwal10-Jan-14 0:49 
GeneralRe: For official holiday Pin
sanjozko28-Feb-17 0:41
sanjozko28-Feb-17 0:41 
GeneralRe: For official holiday Pin
Nitesh Kejriwal28-Feb-17 2:19
professionalNitesh Kejriwal28-Feb-17 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.