SQL确定今天是否是银行假期

时间:2018-08-01 12:49:30

标签: sql stored-procedures

所以我有一个网页,要求用户将时间表输入到完整的每月数据中,但是我要做的是自动将时间表输入到该日期有公众假期的时间表中。帮助。

P.s我有一个表,其中存储了假日,因此我需要匹配今天的日期,如果在那一天是假日,则该框应显示'HH'sql。这是大型sql存储过程的一部分。但是我相信实现会在这里发生。...

DECLARE @DaysInMonth INT

    SELECT @DaysInMonth = DATEPART(dd, DATEADD(dd, -1, DATEADD(mm, 1, CAST(CAST(@Year AS VARCHAR) + '-' + CAST(@Month AS VARCHAR) + '-01' AS DATETIME))))

    DECLARE @Today DATETIME
    DECLARE @LunchStart DATETIME
    DECLARE @LunchEnd DATETIME
    DECLARE @CurrentMonth VARCHAR(16)
    DECLARE @CurrentDay INT
    SELECT @CurrentDay = 1

    SELECT @CurrentMonth = CAST(@Year AS VARCHAR) + '-' + CAST(@Month AS VARCHAR) + '-'

    DECLARE @DailyDataId INT

    WHILE @CurrentDay <= @DaysInMonth
    BEGIN
        SELECT @Today = CONVERT(DATETIME, @CurrentMonth + CAST(@CurrentDay AS VARCHAR),  20)

        -- If it's not a working day don't set a lunchbreak
        IF (DATENAME(weekday, @Today) IN (SELECT WorkingDay FROM dbo.udfGetWorkingDays(@TempTimesheetId)))
        BEGIN
            SELECT @LunchStart = @CurrentMonth + CAST(@CurrentDay AS VARCHAR) + ' ' + CAST(DATEPART(hh, @AMOut) AS VARCHAR) + ':' + CAST(DATEPART(mi, @AMOut) AS VARCHAR)
            SELECT @LunchEnd = @CurrentMonth + CAST(@CurrentDay AS VARCHAR) + ' ' + CAST(DATEPART(hh, @PMIn) AS VARCHAR) + ':' + CAST(DATEPART(mi, @PMIn) AS VARCHAR)
        END
        ELSE
        BEGIN
            SELECT @LunchStart = NULL
            SELECT @LunchEnd = NULL
        END

2 个答案:

答案 0 :(得分:3)

您可以使用EXISTS来检查一天是否为假期,即假期表中存在该天的记录。

IF EXISTS (SELECT *
                  FROM <holiday table>
                  WHERE <day column> = @Today)
BEGIN
  -- special handling for holidays goes here
END;

<holiday table>替换为假期表的名称,并将<day column>替换为该表中保存日期的列。

答案 1 :(得分:1)

我认为假期不需要静态表格, 我目前使用的是什么

您可以添加您在英国的当地假期

CREATE FUNCTION [dbo].[fn_is_it_holiday](@country varchar(3),@date datetime) 
RETURNS bit
AS
BEGIN 
    declare @Tatilmi bit
    if @country in ('TR','TUR','792')
        select @Tatilmi = case 
            when datepart(dw,@date) in (7,1) then 1--Weekends
            when CONVERT(nchar,@date,131) like ' 2/10/%' then 1 --'Ramadan 1st'
            when CONVERT(nchar,@date,131) like ' 3/10/%' then 1 --'Ramadan 2nd'
            when CONVERT(nchar,@date,131) like ' 4/10/%' then 1 --'Ramadan 3rd'
            when CONVERT(nchar,@date,131) like '10/12/%' then 1 --'Sacrifice 1st'
            when CONVERT(nchar,@date,131) like '11/12/%' then 1 --'Sacrifice 2nd'
            when CONVERT(nchar,@date,131) like '12/12/%' then 1 --'Sacrifice 3nd'
            when CONVERT(nchar,@date,131) like '13/12/%' then 1 --'Sacrifice 4th'
            when CONVERT(varchar,@date, 121) like '____-01-01%' then 1 --'New Year'
            when CONVERT(varchar,@date, 121) like '____-04-23%' then 1 --'Children Day'
            when CONVERT(varchar,@date, 121) like '____-05-19%' then 1 --'Youht and Sports Day'
            when CONVERT(varchar,@date, 121) like '____-08-30%' then 1 --'Victory Day'
            when CONVERT(varchar,@date, 121) like '____-10-29%' then 1 --'Republic Day'
        else 0 end
    else if @country in ('US','USA','840')
        select @Tatilmi = case 
            when datepart(dw,@date) in (7,1) then 1--Weekends
            when CONVERT(varchar,@date, 121) like '____-01-01%' then 1 --'New Year'
            when CONVERT(varchar,@date, 121) like '____-07-04%' then 1 --'Independence'
            when CONVERT(varchar,@date, 121) like '____-11-11%' then 1 --'Veterans'
            when CONVERT(varchar,@date, 121) like '____-12-25%' then 1 --'X-Mas 1st'
            when CONVERT(varchar,@date, 121) like '____-12-26%' then 1 --'X-Mas 2st'
            when CONVERT(varchar,@date, 121) like '____-12-27%' then 1 --'X-Mas 3st'
            when CONVERT(varchar,@date, 121) like '____-12-28%' then 1 --'X-Mas 4th'
            when CONVERT(varchar,@date, 121) like '____-12-29%' then 1 --'X-Mas 5th'
            when CONVERT(varchar,@date, 121) like '____-12-30%' then 1 --'X-Mas 6th'
            when CONVERT(varchar,@date, 121) like '____-12-31%' then 1 --'X-Mas 7th'
            when dbo.fn_nth_weekday_of_month(5,4,10,year(@date))=@date then 1--Thanksgiving 
            when dbo.fn_nth_weekday_of_month(2,3,6,year(@date))=@date then 1--Martin Luther King
            when dbo.fn_nth_weekday_of_month(2,3,2,year(@date))=@date then 1--Birth Day of G.Washington and A.Lincoln
            when dbo.fn_nth_weekday_of_month(2,1,9,year(@date))=@date then 1--Workers
            when dbo.fn_nth_weekday_of_month(2,2,10,year(@date))=@date then 1--colombus
        else 0 end
    RETURN @Tatilmi
END


CREATEFUNCTION [dbo].[fn_nth_weekday_of_month](@Weekday int, @nth int, @month int, @year int) 
RETURNS datetime
AS
BEGIN 
    declare @iteration_date datetime, @i int=0
    set @iteration_date = convert(datetime,convert(varchar(4),@year)+'-'+convert(varchar(2),@month)+'-'+'01 00:00')
    while (year(@iteration_date)=@year)and(month(@iteration_date)=@month)
    begin
        if datepart(weekday,@iteration_date)=@Weekday
            set @i = @i + 1
        if @i = @nth
            break;
        set @iteration_date = dateadd(day,1,@iteration_date)
    end
    return @iteration_date
END