UDF to get the count of working days excluding weekend

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 363
CREATE FUNCTION dbo.fn_NumOfWorkingDays
( @p_StartDate DATETIME2,
@p_EndDate DATETIME2)
RETURNS INT
BEGIN
DECLARE @v_wrkng INT = 0;
SELECT @v_wrkng =(DATEDIFF(dd, @p_StartDate, @p_EndDate) + 1) -(DATEDIFF(wk, @p_StartDate, @p_EndDate) * 2)
-(CASE WHEN DATENAME(dw, @p_StartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @p_EndDate) = 'Saturday' THEN 1 ELSE 0 END)

RETURN @v_wrkng
END

Comments or Responses

Login to post response