Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Creating Holidays and Special Dates Expand / Collapse
Author
Message
Posted Tuesday, August 3, 2010 8:37 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 17, 2014 1:28 PM
Points: 176, Visits: 454
Comments posted to this topic are about the item Creating Holidays and Special Dates
Post #963205
Posted Wednesday, August 4, 2010 2:49 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 5:17 AM
Points: 1,274, Visits: 1,985
I needed a similar feature a few years ago and came up with 8 classifications of International holidays (non-working days) as either fixed or relative to some other date, one of which, I think, is unique to the UK. I handled holidays as single days and haven't checked if your multiple day code will cope with this. I assume it will!

The 8 are:
0 -- fixed date every year (MMDD)
1 -- days relative to Easter (e.g. Whitmonday is Easter Sunday + 50 days)
2 -- Fixed, but if it's at a weekend, it's replaced by the following Monday
3 -- Specific day of week, usually Moday of first (MM01) or last (MM25) week of a month, but could be e.g. Wednesday before MMDD
4 -- As 2, but for the 2nd day of a pair (e.g. Christmas/Boxing days in the UK)
5 -- fixed date in Hijri (Moslem) Calendar
6 -- days relative to Greek Orthodox Easter
7 -- relative to previous winter solstice (Chinese Qing Ming Jie)

The awkward item was 4, UK's Boxing Day holiday. Usually it's considered to be 26th December, but if it falls on a Saturday then it's replaced by havig the Monday (28th) as a holiday. However, if it falls on Sunday or Monday, then it's replaced by Tuesday (27/28th) because Christmas Day will be on Saturday/Sunday and hence will have triggered Monday already being a non-working day. So far, this is the only example I've found where a 2-day holiday gives 2 non-working days. Many countries assume that if a 'religious' holiday falls at a weekend, then no extra days are substituted.

I also collapsed the Easter calculation (for 1900 to 2099) into a single inline calculation (which needed a little tweaking):
when 1 -- days relative to Easter (e.g. Whitmonday is Easter Sunday + 50 days)
then
dateadd(d,(case @year when 1954 then 14 when 1981 then 14 when 2049 then 14 when 2076 then 14 else 21 end
+ ((19 * (@year % 19) + 24 ) % 30) + ((2 * (@year % 4) + 4 * (@year % 7) + 6 * ((19 * (@year % 19) +24 ) % 30) + 5) % 7))
+[MonthDayOffset],convert(char(4),@year)+'0301')

I also used a single field as MonthDayOffset, i.e. it either contains a month & day (1225, 0704) or an offset (-2 for Good Friday, 2 days before Easter). This was simply because I could not see any point in a table having 2 columns where either one or the other but never both would be used.

Interesting to see how someone else solved a similar problem.


Derek
Post #963284
Posted Wednesday, August 4, 2010 6:10 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:12 PM
Points: 35,353, Visits: 31,892
I haven't done a deep dive on all the code but, if this works as advertised, it's an awesome solution for the problem stated. Thanks for taking the time to write it all up and sharing it, Terry.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #963396
Posted Wednesday, August 4, 2010 6:15 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 17, 2014 1:28 PM
Points: 176, Visits: 454
Greetings Jeff,

It has been a project I have worked on for several years. I have translated the code several times from programming language to SQL backend to full SQL. Some parts may not be pretty but it should work for you.

Terry Steadman
Post #963398
Posted Wednesday, August 4, 2010 10:45 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 3:24 PM
Points: 17,812, Visits: 15,737
Interesting solution. Thanks for sharing it with us.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #963625
Posted Wednesday, August 4, 2010 11:24 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 3:07 PM
Points: 169, Visits: 1,853
Nice code.

How is it licensed?

Post #963657
Posted Wednesday, August 4, 2010 7:50 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, June 4, 2014 3:25 PM
Points: 436, Visits: 808
Beautiful! Thank you for that very elegant solution to something I've been hashing around with very badly.
Post #963908
Posted Thursday, August 5, 2010 8:02 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 23, 2013 5:53 AM
Points: 3, Visits: 14
Very nice solution. I use a solution like that.

I suggest to include region information in holiday table, because a world wide solution needs to know world, national e local holidays.

We need a solution that can inform international holidays, holidays of a country, holidays of a state and holidays of a city.

For example, a national company that has a solution for dealing with demands. A person can send a request for other peson in different city. Each person has a certain amount of business days to answer a demand. If a person received a demand and he/she works in a city that has a local holiday, her/his deadline for responding it is different from another person in another city that hasn't a local holiday.

Best regards,

Nilson Bonadeu
Post #964283
Posted Thursday, August 5, 2010 8:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 17, 2014 1:28 PM
Points: 176, Visits: 454
Greetings Robert,

You are free to use the code to learn and your personal projects. If you are planning on using it in a commercial product that is to be sold, then I respectfully ask that you talk with me first.

Have a good day.

Terry Steadman
Post #964315
Posted Thursday, August 5, 2010 8:25 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 17, 2014 1:28 PM
Points: 176, Visits: 454
Greetings karyn,

Glad that you were able to find the code useful. If you have any questions, please don't hesitate to ask.

Have a good day.

Terry Steadman
Post #964320
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse