CREATE FUNCTION [dbo].[fn_GetTotalWorkingDays]
(
@DateFrom Date,
@DateTo Date
)
RETURNS INT
AS
BEGIN
DECLARE @TotDays INT= DATEDIFF(DAY, @DateFrom, @DateTo) + 1;
DECLARE @TotWeeks INT= DATEDIFF(WEEK, @DateFrom, @DateTo) * 2;
DECLARE @IsSunday INT= CASE
WHEN DATENAME(WEEKDAY, @DateFrom) = 'Sunday'
THEN 1
ELSE 0
END;
DECLARE @IsSaturday INT= CASE
WHEN DATENAME(WEEKDAY, @DateTo) = 'Saturday'
THEN 1
ELSE 0
END;
DECLARE @TotWorkingDays INT= @TotDays - @TotWeeks - @IsSunday + @IsSaturday;
RETURN @TotWorkingDays;
END
Example :
No comments:
Post a Comment