Following user defined function returns number of weekdays between two dates specified. This function excludes the dates which are passed as input params. It excludes Saturday and Sunday as they are weekends. I always had this function with for reference but after some research I found original source website of the function. This function has been written by Author Alexander Chigrik.
CREATE FUNCTION dbo.spDBA_GetWeekDays
( @StartDate datetime,
@EndDate datetime )
RETURNS INT
AS
BEGIN
DECLARE @WorkDays INT, @FirstPart INT
DECLARE @FirstNum INT, @TotalDays INT
DECLARE @LastNum INT, @LastPart INT
IF (DATEDIFF(DAY, @StartDate, @EndDate) 0) THEN @LastPart - 1
ELSE 0
END
SELECT @WorkDays = @WorkDays * 5 + @FirstNum + @LastNum
END
RETURN ( @WorkDays )
END
GO
This function can be used as
SELECT dbo.spDBA_GetWeekDays ('10/10/2005', '11/22/2005')
GO
Reference : Alexander Chigrik (http://www.mssqlcity.com)






Alexander is good author. Thanks for bringing this to here.
here is the complete text…
CREATE FUNCTION dbo.GetWorkingDays
( @StartDate datetime,
@EndDate datetime )
RETURNS INT
AS
BEGIN
DECLARE @WorkDays int, @FirstPart int
DECLARE @FirstNum int, @TotalDays int
DECLARE @LastNum int, @LastPart int
IF (DATEDIFF(day, @StartDate, @EndDate) < 2)
BEGIN
RETURN ( 0 )
END
SELECT
@TotalDays = DATEDIFF(day, @StartDate, @EndDate) - 1,
@FirstPart = CASE DATENAME(weekday, @StartDate)
WHEN ‘Sunday’ THEN 6
WHEN ‘Monday’ THEN 5
WHEN ‘Tuesday’ THEN 4
WHEN ‘Wednesday’ THEN 3
WHEN ‘Thursday’ THEN 2
WHEN ‘Friday’ THEN 1
WHEN ‘Saturday’ THEN 0
END,
@FirstNum = CASE DATENAME(weekday, @StartDate)
WHEN ‘Sunday’ THEN 5
WHEN ‘Monday’ THEN 4
WHEN ‘Tuesday’ THEN 3
WHEN ‘Wednesday’ THEN 2
WHEN ‘Thursday’ THEN 1
WHEN ‘Friday’ THEN 0
WHEN ‘Saturday’ THEN 0
END
IF (@TotalDays < @FirstPart)
BEGIN
SELECT @WorkDays = @TotalDays
END
ELSE
BEGIN
SELECT @WorkDays = (@TotalDays - @FirstPart) / 7
SELECT @LastPart = (@TotalDays - @FirstPart) % 7
SELECT @LastNum = CASE
WHEN (@LastPart 0) THEN @LastPart - 1
ELSE 0
END
SELECT @WorkDays = @WorkDays * 5 + @FirstNum + @LastNum
END
RETURN ( @WorkDays )
END
GO
also if you want this to be inclusive, add this right below the begin statement and before the first declare
select @StartDate = @StartDate -1
select @EndDate = @EndDate + 1
Missing “=” from line
WHEN (@LastPart 0) THEN @LastPart - 1
Should be
WHEN (@LastPart = 0) THEN @LastPart - 1
Hi. I get the following message when attempting to add your function. Could you please confirm this works for you
Msg 102, Level 15, State 1, Procedure GetWorkingDays, Line 18
Incorrect syntax near ‘@FirstPart’.
Msg 102, Level 15, State 1, Procedure GetWorkingDays, Line 51
Incorrect syntax near ‘END’.
I did one like this:
CREATE FUNCTION [dbo].[WeekdaysCount]
( @StartDate datetime,
@EndDate datetime )
RETURNS INT
AS
BEGIN
DECLARE @TotalDays int, @WorkDays int, @Sats int, @Suns int
IF (DATEDIFF(day, @StartDate, @EndDate) = 0)
BEGIN
RETURN ( 0 )
END
SET @TotalDays = DATEDIFF(day, @StartDate, @EndDate)
SELECT @Sats = DATEDIFF(ww,@StartDate,@EndDate) + CASE WHEN DATENAME(dw,@StartDate) = ’sunday’ THEN 1 ELSE 0 END,
@Suns = DATEDIFF(ww,@StartDate,@EndDate) + CASE WHEN DATENAME(dw,@EndDate) = ’saturday’ THEN 1 ELSE 0 END
SET @WorkDays = @TotalDays - @Sats - @Suns
RETURN ( @WorkDays )
END
GO
how do i taking holidays when calculating number of working days. because i already create table holiday for store all weekend holidays and public holiday.
I think the missing sign should be a ‘>’, not an ‘=’.
i.e.
WHEN (@LastPart > 0) THEN @LastPart - 1
if you want to know weekdays fractional to the second …
create function dbo.WeekDays(@StartDate datetime, @EndDate datetime)
returns float
as
begin
return (
(cast(datediff(s, @StartDate, @EndDate) as float) / 86400)
-(2*DateDiff(ww,@StartDate,@EndDate))+
case when datepart(dw, @StartDate)=7 then 1 else 0 end
)
end
I would like to search for DataLength(AcctNo) = ‘2′ and if first position is = ‘0′ then make AcctNo = 10x where x is second position character of acctno.