C#

get datediff excluding weekends

CREATE FUNCTION FUN_GetDaysExceptWeekEnd(@FromDate date,@ToDate date)
RETURNS int
AS
BEGIN
DECLARE @TotolDays int;
SET @TotolDays=0
select @TotolDays=DATEDIFF(dd, @FromDate, @ToDate) - (DATEDIFF(wk, @FromDate, @ToDate) * 2) -
CASE WHEN DATEPART(dw, @FromDate) = 1 THEN 1 ELSE 0 END +
CASE WHEN DATEPART(dw, @ToDate) = 1 THEN 1 ELSE 0 END
RETURN @TotolDays
END

GO


DECLARE @FromDate datetime
DECLARE @ToDate datetime
SET @FromDate='2020-08-01'
SET @ToDate='2020-08-05'

SELECT DBO.FUN_GetDaysExceptWeekEnd(@FromDate,@ToDate) AS TotalDays


No comments:

Post a Comment