Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to calculate difference in workdays between two dates. Is there a built in function for this in SQL Server? Can someone please provide an example on how to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
149 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is something I wrote quickly. Just encapsulate it into a function or whatever you need.

declare @StartDate datetime
declare @EndDate datetime

declare @TotalDiff int
declare @NumberOfWeekends int

SET @StartDate = '3/12/2013'
SET @EndDate = '3/22/2013'
SET @NumberOfWeekends = 0
SET @TotalDiff = DATEDIFF(d,@StartDate, @EndDate)

If @TotalDiff > 7 
    SET @NumberOfWeekends = @TotalDiff / 7
else if DATEPART(dd, @EndDate) < DATEPART(DD, @StartDate)
    SET @NumberOfWeekends = 1

select (@TotalDiff - 2*@NumberOfWeekends) as TotalWorkDays

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...