Joseph Michael Pesch
VP Programming

TSQL Function to Return Number of Weekdays Between Two Dates (Excludes Weekend Days)

by 4. May 2015 10:41

TSQL function to return a number of weekdays between two dates (i.e. excludes weekend days):

create function WeekdayDateDiff
(
 @BegDt as DATETIME,
 @EndDt as DATETIME
) returns int as begin
  return (datediff(dd, @BegDt, @EndDt) + 1)
  -(datediff(wk, @BegDt, @EndDt) * 2)
  -(case when datepart(dw, @BegDt) = 1 then 1 else 0 end)
  -(case when datepart(dw, @EndDt) = 7 then 1 else 0 end)
end

Tags:

SQL Server

Comments are closed