I am working on a situation where I have a priority list that is associated with a specific amount of business days.
For example, if I had todays date of 2017-01-26
and needed to add 5 business days to it, it would return 2017-02-02
.
To mix in some added functionality, I have a table of company defined holidays that it needs to exclude. If I select 5
business days from 2017-02-16
, it would see that US Presidents day falls on 2017-02-20
and would skip that, making the 5th
business day 2017-02-24
.
I found another example on SO of this which I am trying to adapt to my needs. The problem is, I don't exactly know what is happening, only that my additions are not resulting in what I expect.
Reference Post: https://stackoverflow.com/a/12862675/2628921
ALTER FUNCTION [dbo].[findBusinessDayAfter]
(@date DATETIME, @days INT)
RETURNS DATE
AS
BEGIN
RETURN (SELECT thedate
FROM (SELECT dateadd(d, v.day, CAST (@date AS DATE)) AS thedate,
row_number() OVER ( ORDER BY v.day) AS rn
FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12), (13), (14), (15), (16), (17), (18), (19), (20), (21), (22), (23), (24), (25), (26), (27), (28), (29), (30), (31), (32), (33), (34), (35), (36), (37), (38), (39), (40)) AS v(day)
LEFT OUTER JOIN
holidays AS h
ON h.holidayDate = dateadd(d, v.day, CAST (@date AS DATE))
WHERE h.holidayDate IS NULL
AND LEFT(datename(dw, dateadd(d, v.day, CAST (@date AS DATE))), 1) <> 'S') AS x
WHERE @days = rn);
END
In the original example given, they are using (1) - (10)
. However, I have cases where I will need to add 35-40
business days to a date, so I adjusted the query accordingly to allow me to go up to that.
I have found that when I try to run certain dates with business days, it is returning a null
value and I am not sure why.
-- Multiple Holidays (Day before Thanksgiving)
SELECT dbo.findBusinessDayAfter('2017-11-05', 35) -- Returns NULL
I am not exactly sure what is happening in the original code snippet that would prevent me from being able to increase their default 10 days
to my 30+
days.
Any thoughts on how this could be adapted to fit my use case?