Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating Holidays and Special Dates


Creating Holidays and Special Dates

Author
Message
terrance.steadman
terrance.steadman
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 537
Comments posted to this topic are about the item Creating Holidays and Special Dates
StarNamer
StarNamer
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1294 Visits: 1992
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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44940 Visits: 39859
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
terrance.steadman
terrance.steadman
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 537
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
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21055 Visits: 18258
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

Robert.Sterbal
Robert.Sterbal
SSC Veteran
SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)

Group: General Forum Members
Points: 209 Visits: 2000
Nice code.

How is it licensed?
karyn.webb
karyn.webb
Mr or Mrs. 500
Mr or Mrs. 500 (554 reputation)Mr or Mrs. 500 (554 reputation)Mr or Mrs. 500 (554 reputation)Mr or Mrs. 500 (554 reputation)Mr or Mrs. 500 (554 reputation)Mr or Mrs. 500 (554 reputation)Mr or Mrs. 500 (554 reputation)Mr or Mrs. 500 (554 reputation)

Group: General Forum Members
Points: 554 Visits: 861
Beautiful! Thank you for that very elegant solution to something I've been hashing around with very badly. :-)
nbonadeu
nbonadeu
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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
terrance.steadman
terrance.steadman
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 537
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
terrance.steadman
terrance.steadman
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 537
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
Go


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

































































































































































SQLServerCentral


Search