简体   繁体   English

sql工作日假期

[英]sql working days holidays

I've seen different version of this kind of function for other coding languages (Python, jQuery, etc.) but not for SQL. 我已经看到这种函数的不同版本用于其他编码语言(Python,jQuery等)但不适用于SQL。 I have a procedure that needs to have a date calculated that is 65 days from the creation date, but it cannot include weekend or holidays. 我有一个程序需要计算的日期是从创建日期起65天,但它不能包括周末或假期。 We already have a function that is able to add only working days to a date, but not take into account holidays. 我们已经有一个功能,只能在一个日期添加工作日,但不考虑假期。 We have a holiday table that lists all the holiday dates, tblHolidayDates with a column HolidayDate in standard date format. 我们有一个假期表,其中列出了所有假日日期,tblHolidayDates以及标准日期格式的HolidayDate列。

How would I do this? 我该怎么做? I'd also consider maybe just creating a Calendar table as well if someone could give me a CREATE TABLE query for that - all it would need is dates, weekday, and holiday columns. 我也可以考虑创建一个Calendar表,如果有人可以给我一个CREATE TABLE查询 - 它只需要日期,工作日和假日列。

Below I have given the current loop function that adds business days, but it's missing holidays. 下面我给出了当前的循环函数,它增加了工作日,但却没有假期。 Any help would be greatly appreciated!! 任何帮助将不胜感激!!

ALTER FUNCTION [dbo].[AddWorkDaysToDate]
(   
@fromDate       datetime,
@daysToAdd      int
)
RETURNS datetime
AS
BEGIN   
DECLARE @toDate datetime
DECLARE @daysAdded integer

-- add the days, ignoring weekends (i.e. add working days)
set @daysAdded = 1
set @toDate = @fromDate

while @daysAdded <= @daysToAdd
begin
-- add a day to the to date
set @toDate = DateAdd(day, 1, @toDate)
-- only move on a day if we've hit a week day
if (DatePart(dw, @toDate) != 1) and (DatePart(dw, @toDate) != 7)
begin
    set @daysAdded = @daysAdded + 1
end
end

RETURN @toDate

END

I wrote a SQL function years ago to build a dynamic holiday table as a table function. 几年前我写了一个SQL函数来构建一个动态假日表作为表函数。 The link is below: 链接如下:

http://www.joebooth-consulting.com/sqlServer/sqlServer.html#CalendFunc http://www.joebooth-consulting.com/sqlServer/sqlServer.html#CalendFunc

Hope it helps... 希望能帮助到你...

You can access the table function (or your own holiday table) to determine number of holidays via a SQL statement like below 您可以通过SQL语句(如下所示)访问表函数(或您自己的假期表)以确定假期数

SELECT count(*) FROM holiday_date(2013)
WHERE holiday_date BETWEEN @fromDate AND @toDate

then add the count to the returned date using dateAdd(). 然后使用dateAdd()将计数添加到返回的日期。

If you might have holidays that fall on a weekend, add the following to the WHERE clause 如果您可能在周末有假期,请将以下内容添加到WHERE子句中

   AND DatePart(dw, Holiday_date) != 1) and (DatePart(dw, holiday_date) != 7)

I've always achieved this with a static table of dates from roughly 5 years in the past to 10 in the future, each date being marked with 'working day' status and sometimes other flags as required. 我总是通过一个从过去大约5年到未来10年的静态日期表来实现这一目标,每个日期都标有“工作日”状态,有时还需要其他标志。 Previously using MS-SL server I would achieve this quickly with a WHILE loop, I think MySQL supports the same syntax 以前使用MS-SL服务器我会用WHILE循环快速实现这一点,我认为MySQL支持相同的语法

WHILE (condition)
BEGIN
 INSERT date
END

To create the table either use the Enterprise Manager UI or something like 要创建表,请使用企业管理器UI等

CREATE TABLE DateTable
 (
   actual_date datetime NOT NULL,
   is_holiday bit NOT NULL
 )

Assuming we have a DateTable with columns actual_date (date) and is_holiday (bit) , containing all dates and where all workdays have is_holiday=0: 假设我们有一个DateTable,其列actual_date (date)is_holiday (bit) ,包含所有日期以及所有工作日的is_holiday = 0:

SELECT actual_date from 
(
    SELECT actual_date, ROW_NUMBER() OVER(ORDER BY actual_date) AS Row 
    FROM DateTable
    WHERE is_holiday= 0 and actual_date > '2013-12-01'
) X
WHERE row = 65

This will find 2013-12-01 plus 65 workdays, skipping holidays. 这将发现2013-12-01加上65个工作日,跳过假期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM