CREATE FUNCTION [dbo].[udf_GetWorkingDays](@StartDate DATETIME,@EndDate DATETIME)
RETURNS INT
AS
BEGIN
DECLARE @CurDate DATETIME
DECLARE @i INT
DECLARE @days INT
set @i=1
set @days=0
while @i<=(DATEDIFF(dd, @StartDate, @EndDate))
begin
set @CurDate=dateadd(day,@i, @StartDate)
if(DATENAME(dw, @CurDate) != 'Friday' and DATENAME(dw, @CurDate) != 'Saturday')
begin
set @days=@days+1
end
set @i=@i+1
end
RETURN @days
END
Let's test now with following query:
select dbo.udf_GetWorkingDays('01/01/2011','01/31/2011') as TotalWorkingDays
Result is: 22
Hope this will help others.
No comments:
Post a Comment