## Calculate Holidays

 Author Message PaladinTech SSC-Enthusiastic Group: General Forum Members Points: 108 Visits: 185 Comments posted to this topic are about the item Calculate Holidays Hawkeye67 hgen SSC-Enthusiastic Group: General Forum Members Points: 136 Visits: 88 select * from dbo.GetHolidays(16)ItemID Holiday_Title Holiday_Date1 Easter Sunday 2016-04-102 Thanksgiving 2016-11-24 3 Memorial Day 2016-05-30 4 Labor Day 2016-09-05 select * from dbo.GetHolidays(2016)ItemID Holiday_Title Holiday_Date1 Easter Sunday 2016-03-27 2 Thanksgiving 2016-11-24 3 Memorial Day 2016-05-30 4 Labor Day 2016-09-05 george.p Old Hand Group: General Forum Members Points: 309 Visits: 261 This code is very difficult to understand - there are no supporting comments and a lot of variables named as single letters.Some documentation of how it works would be useful! paulo.margarido Old Hand Group: General Forum Members Points: 393 Visits: 161 An alternative way of doing itbut better was to create a calendar table with relevant columns, like the ones in the CTE's in the function, then its just a select to that table`CREATE FUNCTION dbo.GetHolidays_2( @Year INT = 0 )RETURNS @Holidays TABLE( ItemID INT IDENTITY(1,1) , Holiday_Title VARCHAR(50) , Holiday_Date DATETIME )ASBEGIN DECLARE @Month INT , @Day INT , @Easter DATE -- Calculate Easter Sunday -- Alogrithm modeled after VB2TheMax code DECLARE @g INT , @c INT , @h INT , @i INT , @j INT , @l INT SET @g = @Year % 19 SET @c = @Year / 100 SET @h = ((@c - (@c / 4) - ((8 * @c + 13) / 25) + (19 * @g) + 15) % 30) SET @i = @h - ((@h / 28) * (1 - (@h /28) * (29 / (@h + 1)) * ((21 - @g) / 11))) SET @j = ((@Year + (@Year / 4) + @i + 2 - @c + (@c / 4)) % 7) SET @l = @i - @j SET @Month = 3 + ((@l + 40) / 44) SET @Day = @l + 28 - (31 * (@Month / 4)) SET @Easter = DATEADD(DAY,@Day- 1,DATEADD(MONTH,@Month-1,DATEADD(YEAR, (@Year - DATEPART(YEAR, 0)), 0))) DECLARE @FirstDayOfGivenYear DATE , @LastDayOfGivenYear DATE SELECT @FirstDayOfGivenYear = DATEADD(DAY,0,DATEADD(MONTH,0,DATEADD(YEAR, (@Year - DATEPART(YEAR, 0)), 0))) , @LastDayOfGivenYear = DATEADD(DAY,-1,DATEADD(MONTH,0,DATEADD(YEAR, (@Year - DATEPART(YEAR, 0) + 1), 0))) ;WITH DATES AS ( SELECT @FirstDayOfGivenYear AS date_ , DATEPART(DW, @FirstDayOfGivenYear) AS dw , DATEPART(MONTH, @FirstDayOfGivenYear) AS month_ , DATEPART(WEEK, @FirstDayOfGivenYear) AS week_ , 1 AS ocurrency_in_month UNION ALL SELECT DATEADD(DAY, 1, date_) AS date_ , DATEPART(DW, DATEADD(DAY, 1, date_)) AS dw , DATEPART(MONTH, DATEADD(DAY, 1, date_)) AS month_ , DATEPART(WEEK, DATEADD(DAY, 1, date_)) AS week_ , CASE WHEN DATEPART(DAY, DATEADD(DAY, 1, date_)) = 1 THEN 1 ELSE CASE WHEN DATEPART(DAY, DATEADD(DAY, 1, date_)) IN (8, 15, 22, 29) THEN ocurrency_in_month + 1 ELSE ocurrency_in_month END END AS ocurrency_in_month FROM DATES WHERE date_ < @LastDayOfGivenYear ) , DATES_2 AS ( SELECT D.date_ , D.dw , D.month_ , D.week_ , D.ocurrency_in_month , M.max_ocurrency_in_month FROM DATES D INNER JOIN ( SELECT month_ , dw , MAX(ocurrency_in_month) AS max_ocurrency_in_month FROM DATES GROUP BY month_, dw ) M ON M.month_ = D.month_ and M.dw = D.dw ) , HOLLIDAYS AS ( SELECT 'Easter Sunday' AS Holiday_Title , @Easter AS Holiday_Date UNION ALL SELECT CASE WHEN month_ = 11 AND dw = 5 AND ocurrency_in_month = 4 THEN 'Thanksgiving' -- 4º Thursday of November WHEN month_ = 9 AND dw = 2 AND ocurrency_in_month = 1 THEN 'Labor Day' -- 1º Monday of September WHEN month_ = 5 AND dw = 2 AND ocurrency_in_month = max_ocurrency_in_month THEN 'Memorial Day' -- Last Monday of May END AS Holiday_Title , date_ AS Holiday_Date FROM DATES_2 ) INSERT INTO @Holidays (Holiday_Title, Holiday_Date) SELECT Holiday_Title, Holiday_Date FROM HOLLIDAYS WHERE Holiday_Title IS NOT NULL OPTION (MAXRECURSION 0) RETURNEND` Iwas Bornready SSC-Insane Group: General Forum Members Points: 21902 Visits: 885 george.p (11/24/2016)This code is very difficult to understand - there are no supporting comments and a lot of variables named as single letters.Some documentation of how it works would be useful!I agree. Still kind of nice though. PaladinTech SSC-Enthusiastic Group: General Forum Members Points: 108 Visits: 185 I just became aware of the fact that SQL Server Central subscribers were still accessing this version of Calculate Holidays. This was written years ago and I have since done many different versions of this, including preparing an actual table of holidays, for my clients. I was also aware of the fact of the difference between using the two digit year and the four digit year. I will try and find my most recent version for SQL Server Central posting.Sorry for the late response. I am semi-retired and spending a lot of time hunting the "king" of game birds, the Ruffed Grouse, with my new hunting dog.Take care.PaladinTechI don't understand why I am called a "rookie" when I have been programming SQL Server since the introduction of version 6.5? Hawkeye67

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 14 - Administration      SQL Server 14 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      Question of the Day (QOD)      SQL Server Security Skills Microsoft Access      Microsoft Access Products and Books      Third Party Products         CA         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         SQLCentric         Golden Gate Software         Lumigent         Red Gate Software         Quest Software         ApexSQL         Idera      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 7.0 Books         Discuss SQL Server 2000 Books Notification Services      Administration Article Discussions Future Versions      SQL 12