ALTER function [dbo].[fn_WorkDays](@begin datetime,@end datetime)
returns int
As
BEGIN
declare @i int,@j int ,@discrepancy int
set @j=0
set @discrepancy=datediff(d,@begin,@end)
if @end>@begin
begin
set @i=1
while @i<=@discrepancy
begin
if datepart(weekday,dateadd(d,@i,@begin)) not in(1,7)
set @j=@j+1
set @i=@i+1
end
end
else if @end<@begin
begin
set @i=-1
while @i>=@discrepancy
begin
if datepart(weekday,dateadd(d,@i,@begin)) not in(1,7)
set @j=@j+1
set @i=@i-1
end
end
if @discrepancy<0
begin
set @j=-@j
end
return @j
END