Jeff Smith Blog

Random Thoughts & Cartesian Products with Microsoft SQL Server

How To Calculate the Number of Week Days Between two Dates

If the start date and end date are both week days, then the total number of week days in between is simply:

(total difference in days) - (total difference in weeks) * 2

or
 DateDiff(dd, @start, @end) - DateDiff(ww, @start, @end)*2

… since the DateDiff() function with weeks returns the number of week "boundaries" that are crossed; i.e., the number of weekends.

If you have a table of holidays, then you can simply subtract them out as well:

DateDiff(dd, @start, @end) -
DateDiff(ww, @start, @end)2 - 
(select count(
) from holidays where holiday_date between @start and @end)

Now, what if the start day or the end day is on a weekend?  In that case, you need to define what to do in those situations in your requirements.

For example, if the start date is Sunday, Nov 20th, and the end day is Monday, Nov 21st – how many week days are between those dates? There's no universal correct answer; it could be 0, or 1, or perhaps even "undefined" (null) depending on your needs.

Legacy Comments


Wayne West
2008-08-06
re: How To Calculate the Number of Week Days Between two Dates
I like your approach, but for my needs, I also need to include time in my calculation in addition to excluding weekends. If @End is 8/6 11am and @Start is 8/5 11:30am, I need a zero. I also have to include government holidays. So I think I'll be sticking with my lookup tables.

lucindom
2008-08-07
re: How To Calculate the Number of Week Days Between two Dates
Yo uso la siguiente Instruccion:
(I use the next sentences:)

--- Declar the Dates
DECLARE @start As DateTime, @end As DateTime
--- Asign some values
SET @start=getDate()
SET @end=getDate()-25
--- Convert to DATE without time ( DD/MM/YYYY o regional format)

SELECT @start=CONVERT(datetime,LEFT(CONVERT(varchar,@start,120),10),120)
SELECT @end=CONVERT(datetime,LEFT(CONVERT(varchar,@end,120),10),120)

SELECT @start,@end

Muthu
2008-09-16
re: How To Calculate the Number of Week Days Between two Dates
Hi I need to to calculate number of months,weeks,quartyearly and half yearly between two dates in ms sql can any one help

sumi
2009-01-15
re: How To Calculate the total Number of Days Between two Dates, asp.net
thanks, for ya patience

Jan
2009-01-30
re: How To Calculate the Number of Week Days Between two Dates
Hi,
I am using SQL script into MS Excel,
in stead of assigning value as below:
SET @start=getDate()
can I assign @start to refer to a cell in MS Excel worksheet?
Thanks...

ali rizwan
2009-02-06
re: How To Calculate the Number of Week Days Between two Dates
I don't think this approach works with two dates on the weekends still. I tried it using the dates 3/1/08 as start date and 3/9/08 as end date and I get 3 days as my days.

Sundar
2009-03-25
re: How To Calculate the Number of Week Days Between two Dates
SELECT DATEDIFF(d,@StartDate,@EndDate)+1
- (DATEDIFF(wk,@StartDate,@EndDate) + CASE WHEN DATEPART(dw,@StartDate)=1 then 1 else 0 End )
- (DATEDIFF(wk,@StartDate,@EndDate) + CASE WHEN DATEPART(dw,@EndDate)=7 then 1 else 0 End )

ERTERTRE
2009-05-18
re: How To Calculate the Number of Week Days Between two Dates
etertretertret

Lizzy & Lorna
2009-06-18
re: How To Calculate the Number of Week Days Between two Dates
Thanks for this it works a treat , saved us loads of brain ache.

Would it be too much to ask how you also count bank holidays?

xyz
2009-06-18
re: How To Calculate the Number of Week Days Between two Dates
Lizzy & Lorna -- read the entire article, perhaps?

viji vijayan
2009-12-23
how to calculate difference between two dates?
hai i need to calculate date differences in mysql

ekhar
2010-01-07
re: How To Calculate the Number of Week Days Between two Dates
hi. can you help me to know. how many saturday will be in jan 1-15. something like that.

mahendra
2010-08-13
Average number of transaction per week
hi friends,

how to calculate average number of transactions done per week and per month in mysql