How do I determine a public holiday in Sql server?

前端 未结 24 1867
慢半拍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:36

    Let's simplify this:


    case
    
    -- New Year's Day
    when DATEPART(MM, @DATE) = 1
        and DATEPART(DD, @DATE) = 1
        and DATEPART(DW, @DATE) in (2,3,4,5,6) then 'Y'
    when DATEPART(MM, @DATE) = 12
        and DATEPART(DD, @DATE) = 31
        and DATEPART(DW, @DATE) = 6 then 'Y'
    when DATEPART(MM, @DATE) = 1
        and DATEPART(DD, @DATE) = 2
        and DATEPART(DW, @DATE) = 2 then 'Y'
    
    -- Memorial Day (last Monday in May)
    when DATEPART(MM, @DATE) = 5
        and DATEPART(DD, @DATE) between 25 and 31
        and DATEPART(DW, @DATE) = 2 then 'Y'
    
    -- Independence Day
    when DATEPART(MM, @DATE) = 7
        and DATEPART(DD, @DATE) = 4
        and DATEPART(DW, @DATE) in (2,3,4,5,6) then 'Y'
    when DATEPART(MM, @DATE) = 7
        and DATEPART(DD, @DATE) = 3
        and DATEPART(DW, @DATE) = 6 then 'Y'
    when DATEPART(MM, @DATE) = 7
        and DATEPART(DD, @DATE) = 5
        and DATEPART(DW, @DATE) = 2 then 'Y'
    
    -- Labor Day (first Monday in September)
    when DATEPART(MM, @DATE) = 9
        and DATEPART(DD, @DATE) between 1 and 7
        and DATEPART(DW, @DATE) = 2 then 'Y'
    
    -- Thanksgiving Day (fourth Thursday in November)
    when DATEPART(MM, @DATE) = 11
        and DATEPART(DD, @DATE) between 22 and 28
        and DATEPART(DW, @DATE) = 5 then 'Y'
    
    -- Black Friday (day after Thanksgiving)
    when DATEPART(MM, @DATE) = 11
        and DATEPART(DD, @DATE) between 23 and 29
        and DATEPART(DW, @DATE) = 6 then 'Y'
    
    -- Christmas Day
    when DATEPART(MM, @DATE) = 12
        and DATEPART(DD, @DATE) = 25
        and DATEPART(DW, @DATE) in (2,3,4,5,6) then 'Y'
    when DATEPART(MM, @DATE) = 12
        and DATEPART(DD, @DATE) = 24
        and DATEPART(DW, @DATE) = 6 then 'Y'
    when DATEPART(MM, @DATE) = 12
        and DATEPART(DD, @DATE) = 26
        and DATEPART(DW, @DATE) = 2 then 'Y'
    
    else 'N' end
    

提交回复
热议问题