makeitwork09
Technical User
I am using SQL Server 2005
We created the following function, which change the @date_to_check to the next business day, if that date is on a weekend or a holiday.
NOTE, the procedure that calls the function returns 70,000+ records and with the following code the stored procedure returns records in about 15 seconds.
However, when the above was modified so that if the @date_to_check was on the 10th the previous business day returns, rather than the next business day, the query that calls the function went from running in 15 seconds to after 11 minutes still not returning any records.
The modified code looks as follows:
How can this function be modfied to avoid performance issues?
Plesae not too, that is test environment is using VMWare.
Thanks
We created the following function, which change the @date_to_check to the next business day, if that date is on a weekend or a holiday.
NOTE, the procedure that calls the function returns 70,000+ records and with the following code the stored procedure returns records in about 15 seconds.
Code:
ALTER FUNCTION [dbo].[get_next_bus_day_udf](@date_to_check datetime ) RETURNS datetime
AS
BEGIN
-- Then Check if next day is weekend or holiday, keep incrementing to next business day:
WHILE DATENAME(weekday, @date_to_check) = 'Saturday' or
DATENAME(weekday, @date_to_check) = 'Sunday' or
@date_to_check in (select HOLIDAY from lms_nyl..globcal where country_code = 'USA' )
select @date_to_check = dateadd(day,1,@date_to_check)
return @date_to_check
END
However, when the above was modified so that if the @date_to_check was on the 10th the previous business day returns, rather than the next business day, the query that calls the function went from running in 15 seconds to after 11 minutes still not returning any records.
The modified code looks as follows:
Code:
ALTER FUNCTION [dbo].[get_next_bus_day_udf](@date_to_check datetime ) RETURNS datetime
AS
BEGIN
-- Then Check if next day is weekend or holiday, keep incrementing to next business day:
WHILE DATENAME(weekday, @date_to_check) = 'Saturday' or
DATENAME(weekday, @date_to_check) = 'Sunday' or
@date_to_check in (select HOLIDAY from lms_nyl..globcal where country_code = 'USA' )
select @date_to_check = dateadd(day,case when datepart(day,@date_to_check) = 10 then -1 else 1 end,@date_to_check)
return @date_to_check
END
How can this function be modfied to avoid performance issues?
Plesae not too, that is test environment is using VMWare.
Thanks