USE [P21]
GO

/****** Object:  UserDefinedFunction [dbo].[nutech_fn_DateAfterNumberWorkDays]    Script Date: 9/23/2024 6:43:53 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		NuTech Services, LLC.
-- Create date: 02/27/2019
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[nutech_fn_DateAfterNumberWorkDays]
(
	@prevstartdate Datetime,
	@NumberofWorkDays Int
)
RETURNS DateTime
AS
BEGIN

	DECLARE @prevenddate DateTime

	Select @NumberofWorkDays = @NumberofWorkDays - 1

	SELECT @prevenddate =
		DATEADD(day, (@NumberofWorkDays % 5) + CASE ((@@DATEFIRST + DATEPART(weekday, @prevstartdate) + (@NumberofWorkDays % 5)) % 7)
                                    WHEN 0 THEN 2
                                    WHEN 1 THEN 1
                                    ELSE 0 END, DATEADD(week, (@NumberofWorkDays / 5), @prevstartdate))

	RETURN @prevenddate

END
GO


