Blog

Calculate Number of Working Days Between Two Date by Using SQL

Posted by Akash Garg Blogs | SQL Apr 09, 2013
In this blog we calculate the working days in a company by using SQL.

Introduction

This Blog will help you to find out the working days between two dates.

Description

Here @startdate indicate the first date and @enddate indicate  the last date, by these two date we can calculate the working days for a company/organization. In this code we use two case, first case is used for Saturday and other case is used for Sunday Where we use Saturday and Sunday As non working days. It will help you to exclude these two day's from the working days.

The Code is shown Below

DECLARE @startdate DATETIME
DECLARE
@enddate DATETIME
SET
@startdate = '2013/04/01'
SET
@enddate = '2013/04/30'
SELECT
  
(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)
as WorkingDays


Output

sql2.jpg

COMMENT USING
PREMIUM SPONSORS
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.