I\'ve seen different version of this kind of function for other coding languages (Python, jQuery, etc.) but not for SQL. I have a procedure that needs to have a date calcul
I wrote a SQL function years ago to build a dynamic holiday table as a table function. The link is below:
http://www.joebooth-consulting.com/sqlServer/sqlServer.html#CalendFunc
Hope it helps...
You can access the table function (or your own holiday table) to determine number of holidays via a SQL statement like below
SELECT count(*) FROM holiday_date(2013)
WHERE holiday_date BETWEEN @fromDate AND @toDate
then add the count to the returned date using dateAdd().
If you might have holidays that fall on a weekend, add the following to the WHERE clause
AND DatePart(dw, Holiday_date) != 1) and (DatePart(dw, holiday_date) != 7)
Assuming we have a DateTable with columns actual_date (date)
and is_holiday (bit)
, containing all dates and where all workdays have is_holiday=0:
SELECT actual_date from
(
SELECT actual_date, ROW_NUMBER() OVER(ORDER BY actual_date) AS Row
FROM DateTable
WHERE is_holiday= 0 and actual_date > '2013-12-01'
) X
WHERE row = 65
This will find 2013-12-01 plus 65 workdays, skipping holidays.
I've always achieved this with a static table of dates from roughly 5 years in the past to 10 in the future, each date being marked with 'working day' status and sometimes other flags as required. Previously using MS-SL server I would achieve this quickly with a WHILE loop, I think MySQL supports the same syntax
WHILE (condition)
BEGIN
INSERT date
END
To create the table either use the Enterprise Manager UI or something like
CREATE TABLE DateTable
(
actual_date datetime NOT NULL,
is_holiday bit NOT NULL
)