I have a table of dates call [BadDates], it has just one column where every record is a date to exclude. I have a UDF as follows:
CREATE FUNCTION [dbo].[udf_GetDateInBusinessDays]
(
@StartDate datetime, --Start Date
@NumberDays int --Good days ahead
)
RETURNS datetime
AS
BEGIN
-- Declare the return variable here
DECLARE @ReturnDate datetime
SET @ReturnDate = @StartDate
DECLARE @Counter int
SET @Counter = 0
WHILE @Counter < @NumberDays
BEGIN
SET @ReturnDate = DateAdd(d,1,@ReturnDate)
IF ((SELECT COUNT(ID)
FROM dbo.[BadDates]
WHERE StartDate = @ReturnDate) = 0)
BEGIN
SET @Counter = @Counter + 1
END
END
RETURN @ReturnDate
END
This UDF works great, but it is slow in processing. The stored procedure that uses this runs the UDF in every record. Is there other ways to provide this same functionality in a faster method.
Any help is greatly appreciated!