How do I determine a public holiday in Sql server?

前端 未结 24 1841
慢半拍i
慢半拍i 2020-12-05 14:40

I have an application written in c# that cannot run on a public holiday or a weekend. I\'ve looked around a bit and haven\'t found anywhere (official) that provides all the

相关标签:
24条回答
  • 2020-12-05 15:11

    Isn't a public holiday very dependent of locale?

    Programatically, there is no way to compute this. Alternatively, you will need to find an official list of holidays for each of your locales. And even so, you will not get the granularity you want.

    0 讨论(0)
  • 2020-12-05 15:11

    Found another service for worldwide public holidays, allegedly used by calendar publishers:

    http://www.qppstudio.net/index.htm

    0 讨论(0)
  • 2020-12-05 15:12

    For many of us in the USA, there is a standard list of business holidays which can be deterministically calculated.

    Drawing inspiration from Vonpato's code, I put together a single SQL statement that makes use of Window functions ( over(partition by ..) ), new in SQL Server 2008, to determine the N'th or last occurrence of a weekday within a month.

    This should be run against "DimDate", a standard Date Dimension table such as found in a Data Warehouse. http://www.codeproject.com/Articles/647950/Create-and-Populate-Date-Dimension-for-Data-Wareho

    SELECT 
        case 
            when DayNameOfWeek = 'Saturday' then dateadd(day, -1, FullDate) 
            when DayNameOfWeek = 'Sunday' then dateadd(day, 1, FullDate) 
        else FullDate end as 'Holiday Date'
    FROM (
        select row_number() over (partition by DayNameOfWeek, MonthOfYear, CalendarYear order by FullDate) as WeekdayOrdinal,
        count(*) over (partition by DayNameOfWeek, MonthOfYear, CalendarYear ) as MaxOrdinal,    *  
        from DimDate  
    ) D
    WHERE
            (D.[MonthName] = 'January'  and [DayOfMonth]  = 1)                                              /* New Years Day    */
        OR  (D.[MonthName] = 'January'  and DayNameOfWeek = 'Monday')   and WeekdayOrdinal = 3              /* MLK Day          */
        OR  (D.[MonthName] = 'February' and DayNameOfWeek = 'Monday')   and WeekdayOrdinal = 3              /* President's Day  */
        OR  (D.[MonthName] = 'May'      and DayNameOfWeek = 'Monday')   and WeekdayOrdinal = MaxOrdinal     /* Memorial Day     */
        OR  (D.[MonthName] = 'September' and DayNameOfWeek = 'Monday')  and WeekdayOrdinal = 1              /* Labor Day        */
        OR  (D.[MonthName] = 'October'  and DayNameOfWeek = 'Monday')   and WeekdayOrdinal = 2              /* Columbus Day     */
        OR  (D.[MonthName] = 'November' and [DayOfMonth] = 11)                                              /* Veteran's Day    */
        OR  (D.[MonthName] = 'November' and DayNameOfWeek = 'Thursday') and WeekdayOrdinal = 4              /* Thanksgiving     */
        OR  (D.[MonthName] = 'December' and [DayOfMonth]  = 25 )                                            /* Christmas        */
    ORDER BY FullDate
    
    0 讨论(0)
  • 2020-12-05 15:12

    If it's just England, then you can work them out for yourself! You'll need to get a reliable algorithm for determining Easter, but otherwise I'd say you could do it in under an hour.

    But do you mean just England, or the UK? Because Scotland has different holidays (Christmas, Hogmanay and St Andrew's Day) and Northern Ireland, Wales and most likely the Isle of Man and the Channel Islands should also be traded differently.

    As noted elsewhere, once your scope gets wider then it's even more complex. There are local holidays, half-days, days when banks are open but stock exchanges not, all kinds of horrors.

    If you really can't manage holidays yourself and don't have users who can be given responsibility, then I'd suggest going back to your "can't run on a public holiday" constraint and looking for ways in which that might be removed...

    0 讨论(0)
  • 2020-12-05 15:13

    You can user the Nager.Date library for calculate public holidays. https://github.com/nager/Nager.Date

    Nuget

    PM> install-package Nager.Date
    

    Example:

    var publicHolidays = DateSystem.GetPublicHoliday(CountryCode.GB, 2017);
    foreach (var publicHoliday in publicHolidays)
    {
        var name = publicHoliday.LocalName;
    }
    
    0 讨论(0)
  • 2020-12-05 15:13

    In addition to allowing the user to configure what days are holidays, it would be nice if you allowed the user to select a calendar to import or even to subscribe to. iCalShare has a nice list. However, it's probably too much work for a feature that's merely nice.

    0 讨论(0)
提交回复
热议问题