Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in SQL by (20.3k points)

How can I calculate the number of workdays between two dates in SQL Server?

Monday to Friday and it must be T-SQL.

1 Answer

0 votes
by (40.7k points)

Monday to Friday, for workdays you can do it with a single SELECT, this way:

DECLARE @StartDate DATETIME

DECLARE @EndDate DATETIME

SET @StartDate = '2008/10/01'

SET @EndDate = '2008/10/31'

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)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 15, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...