Tuesday, December 2, 2014

recursive CTE to get first business day excluding public holidays



If all you need is a first business day of the year barring public holidays , then this would be enough



DECLARE @Year int = 2014 --sample value

DECLARE @StartDate datetime,@EndDate datetime

SELECT @StartDate = DATEADD(yy,@Year-1900,0),
@EndDate = DATEADD(yy,1,@StartDate)-1

SELECT MIN([Date])
FROM dbo.CalendarTable(@StartDate,@EndDate,1,0) f
WHERE NOT EXISTS (
SELECT 1
FROM PublicHolidayTable
WHERE Date <> f.Date
)

UDF can be found here


http://ift.tt/1kpjgTF




Please Mark This As Answer if it solved your issue

Please Mark This As Helpful if it helps to solve your issue

Visakh

----------------------------

My MSDN Page

My Personal Blog

My Facebook Page


No comments:

Post a Comment