Creating Holidays and Special Dates

  • terrance.steadman

    SSCrazy

    Points: 2097

    Comments posted to this topic are about the item Creating Holidays and Special Dates

  • StarNamer

    SSCrazy Eights

    Points: 8573

    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

    SSC Guru

    Points: 993628

    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.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

    Helpful Links:
    How to post code problems

  • terrance.steadman

    SSCrazy

    Points: 2097

    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

    SSC Guru

    Points: 281079

    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[/url]
    Learn Extended Events

  • Robert.Sterbal

    SSCrazy

    Points: 2845

    Nice code.

    How is it licensed?

  • karyn.webb

    SSChasing Mays

    Points: 618

    Beautiful! Thank you for that very elegant solution to something I've been hashing around with very badly. 🙂

  • nbonadeu

    SSC Rookie

    Points: 49

    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

    SSCrazy

    Points: 2097

    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

    SSCrazy

    Points: 2097

    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

  • nbonadeu

    SSC Rookie

    Points: 49

    I wrote a wrong text into a reply few minutes ago.

    The correct text is: (I included the word NOT in the last phrase)

    For example, a national company that has a solution for dealing with demands. A person can send a request for other in different city, if necessary. Each person has a certain amount of business days to answer a demand. If a person receiving a demand works a city that has a local holiday his deadline for responding is different from another person in another city that has NOT a local holiday.

  • terrance.steadman

    SSCrazy

    Points: 2097

    Greetings nbonadeu,

    The regional idea is very nice. I have not worried about that yet though as I have not needed it. It would probably take another table to link the holiday to what regions celebrate that holiday. I don't think that a single field would be enough as more than one region may celebrate a holiday, but few holidays are actually celebrated by all regions. This link table can then be used to decide if a holiday should be included or excluded when deciding the number of business days for a particular area.

    Thank you for the suggestion.

    Terry Steadman

  • nbonadeu

    SSC Rookie

    Points: 49

    Terry,

    You are right. To create a complete solution to deal with holidays may be we will need a region table and a third table to create a relation between holiday and region.

    But I believe we can create much simpler solution, also satisfactory, creating only one table region and placing region information on the holiday table.

    In this case we would use the solution only in one country. Each national or International holiday would be associated with the local country. Each state holiday associated with appropriated state and every city holiday associated with appropriated city.

    If it were necessary that a holiday is associated with two or more states or cities, because it is an exception, we would create a row in the holidays table for each one.

    Best regards,

    Nilson Bonadeu

  • Rick.Cornell.SME

    SSCrazy Eights

    Points: 8577

    I agree... Way to go....

  • wagner crivelini

    SSC Eights!

    Points: 858

    great idea. thanks for taking the time to share it with us

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply