3

我需要一个 MySQL 函数,它允许我通过多个工作日(周一至周五)和一个开始 DATE 或 DATETIME(对我的实现无关紧要),并让它返回一个新的 DATE 或 DATETIME 多个工作日在将来。

示例:SELECT AddWorkDays(10, "2013-09-01")假设“2013-09-01”是星期一,则返回“2013-09-16”。

同样:SELECT AddWorkDays(-10, "2013-09-16")返回“2013-09-01”

我为一个 MSSQL 数据库(我认为)找到了这个函数,这正是我需要的,除了它不在 MySQL 中。我试图手动将其转换为 MySQL 语法并做到了这一点:

DROP FUNCTION IF EXISTS AddWorkDays;
DELIMITER $$
CREATE FUNCTION AddWorkDays
(
    WorkingDays INT,
    StartDate DATE
)
RETURNS DATE

BEGIN
    DECLARE Count INT;
    DECLARE i INT;
    DECLARE NewDate DATE;
    SET Count = 0;
    SET i = 0;

    WHILE (i < WorkingDays) DO
        BEGIN
            SET Count = Count + 1;
            SET i = i + 1;
            WHILE DAYOFWEEK(ADDDATE(StartDate, Count)) IN (1,7) DO
                BEGIN
                    SET Count = Count + 1;
                END;
            END WHILE;
        END;
    END WHILE;

    SET NewDate = ADDDATE(StartDate, Count);
    RETURN NewDate;

END;
$$

DELIMITER ;

我最终得到一个错误:

Error 1415: Not allowed to return a result set from a function

我似乎无法弄清楚它试图返回结果集的确切位置。

我的语法有错误吗?有没有更好的解决方案?

谢谢!

编辑

看来 MySQL 没有 DATEPART 或 DATEADD 函数。我在文档中看到他们有 ADDDATE 和 DAYOFWEEK。更新了代码以表示这一点。我还将 SELECT 语句更改为 SET (现在我得到原始错误的原因是有道理的)

结果,尝试通过 CF 使用该函数运行查询时出现新错误

[Table (rows 1 columns ADDWORKDAYS(10,"2013-09-01")): [ADDWORKDAYS(10,"2013-09-01"): coldfusion.sql.QueryColumn@7a010] ] is not indexable by ADDWORKDAYS(10
4

8 回答 8

10

这是使用 mysql 语法的新函数:

DROP FUNCTION IF EXISTS AddWorkDays;
DELIMITER $$
CREATE FUNCTION AddWorkDays
(
    WorkingDays INT,
    StartDate DATETIME
)
RETURNS DATETIME

BEGIN
    DECLARE Count INT;
    DECLARE i INT;
    DECLARE NewDate DATETIME;
    SET Count = 0;
    SET i = 0;

    WHILE (i < WorkingDays) DO
        BEGIN
            SELECT Count + 1 INTO Count;
            SELECT i + 1 INTO i;
            WHILE DAYOFWEEK(DATE_ADD(StartDate,INTERVAL Count DAY)) IN (1,7) DO
                BEGIN
                    SELECT Count + 1 INTO Count;
                END;
            END WHILE;
        END;
    END WHILE;

    SELECT DATE_ADD(StartDate,INTERVAL Count DAY) INTO NewDate;
    RETURN NewDate;

END;
$$

DELIMITER ;
于 2013-07-18T18:50:26.360 回答
2

这种实现比公认的答案更有效(可能不重要),但也适用于负面工作日(对我来说很重要)。

基本思想是每 5 天转换为 7 天,然后如果 (days % 5) + 一周的开始日不是工作日,您可能需要通过增加或减少 2 天来进行调整。

DROP FUNCTION IF EXISTS AddBusDays;
DELIMITER $$
CREATE FUNCTION AddBusDays
(
    WorkingDays INT,
    UtcStartDate DATETIME,
    TZ VARCHAR(1024) 
)
RETURNS DATETIME

BEGIN
DECLARE RealOffset INT;
DECLARE StartDate DATETIME;
DECLARE Adjustment INT;

SELECT CONVERT_TZ(UtcStartDate, 'UTC', TZ) into StartDate;

select case when WorkingDays >=0 then 2 else -2 end into Adjustment;

select 
    case when (WorkingDays >= 0 AND DAYOFWEEK(StartDate) + (WorkingDays %  5) > 6) OR (WorkingDays < 0 AND DAYOFWEEK(StartDate) + (WorkingDays %  5) < 2)
    then (WorkingDays %  5) + Adjustment + (WorkingDays DIV 5) * 7
    else WorkingDays %  5 + (WorkingDays DIV 5) * 7
    end into RealOffset;
return CONVERT_TZ(date(adddate(StartDate, RealOffset)), TZ, 'UTC');
END;
$$

DELIMITER ;
于 2015-10-08T22:28:09.120 回答
1
-- This is exact query which adds no of business days to date (Exclude Saturday and Sunday)   
 DROP FUNCTION IF EXISTS DateAddBusiness;
    DELIMITER ||
    CREATE FUNCTION DateAddBusiness(mydate DATE, numday INT) 
    RETURNS DATE
    DETERMINISTIC
    COMMENT 'Adds business days between two dates'
    BEGIN
     DECLARE num_week INT DEFAULT 0;
     DECLARE num_day INT DEFAULT 0;
     DECLARE adj INT DEFAULT 0;
     DECLARE total INT DEFAULT 0;
     SET num_week = numday DIV 5;
     SET num_day = MOD(numday, 5);

     IF (DAYOFWEEK(mydate)=6 || DAYOFWEEK(mydate)=5 || DAYOFWEEK(mydate)=4 ) then
      SET adj = 2;
     END IF;

     IF (DAYOFWEEK(mydate)=7 ) then
      SET adj = 1;
     END IF;

     SET total = adj + num_day;
     RETURN DATE_ADD(mydate, INTERVAL total DAY);
    END
    ||
    DELIMITER ;


    -- Unit testing queries
    select DateAddBusiness("2015-10-19","3") // 22
    select DateAddBusiness("2015-10-20","3") // 23

    select DateAddBusiness("2015-10-21","3") // 26
    select DateAddBusiness("2015-10-22","3") // 27
    select DateAddBusiness("2015-10-23","3") // 28
    select DateAddBusiness("2015-10-24","3") //28
    select DateAddBusiness("2015-10-25","3") //28
    select DateAddBusiness("2015-10-26","3") //29
于 2015-10-26T05:37:10.140 回答
1

我修改了此处给出的版本以接受正面和负面的日子。其他答案都没有为我做,所以这是我想出的最有效的解决方案。

DROP FUNCTION IF EXISTS WORKDAY_ADD;
DELIMITER &&
CREATE FUNCTION WORKDAY_ADD(mydate DATE, numday INT) RETURNS DATE
BEGIN
DECLARE num_week INT DEFAULT 0;
DECLARE num_day INT DEFAULT 0;
DECLARE adj INT DEFAULT 0;
DECLARE total INT DEFAULT 0;
SET num_week = ABS(numday DIV 5);
SET num_day = MOD(numday, 5);

IF (WEEKDAY(DATE_ADD(mydate, INTERVAL num_day DAY)) >= 5) THEN 
    SET adj = 2;
END IF;

SET total = (num_week * 7 + adj + ABS(num_day));

IF numday < 0 THEN
    SET total = total * -1;
END IF;

RETURN DATE_ADD(mydate, INTERVAL total DAY); 
    
END&&
DELIMITER ;

用法

加五个工作日:SELECT WORKDAY_ADD('2016-02-18', 5)
减去两个工作日:SELECT WORKDAY_ADD('2016-02-18', -2)

于 2016-02-18T17:13:07.753 回答
0

在这篇文章中,有一个功能可以满足您的要求。我认为该功能可以帮助您。

于 2013-07-18T19:33:13.837 回答
0
CREATE FUNCTION `WORKDAY_ADD`(exp_date DATE, days_to_add SMALLINT) 
RETURNS date
     DETERMINISTIC
BEGIN
    DECLARE res_date DATE;
    DECLARE day_count TINYINT;

SET res_date = date_add(exp_date, INTERVAL 1 DAY); 
IF DAYNAME(res_date)='Monday' OR DAYNAME(res_date)='Tuesday' OR DAYNAME(res_date)='Wednesday' THEN
    SET res_date = date_add(exp_date, INTERVAL days_to_add DAY);
ELSEIF DAYNAME(res_date)='Thursday' OR DAYNAME(res_date)='Friday' OR DAYNAME(res_date)='Saturday' THEN
    SET res_date = date_add(exp_date, INTERVAL days_to_add+2 DAY);        
ELSEIF DAYNAME(res_date)='Sunday' THEN
    SET res_date = date_add(exp_date, INTERVAL days_to_add+1 DAY); 
END IF;
  RETURN res_date;
END;
于 2017-06-15T12:00:32.837 回答
0
FUNCTION `WORKDAY_ADD`(mydate DATE, numday INT) RETURNS date
BEGIN
DECLARE num_day INT DEFAULT 0;
DECLARE adj INT DEFAULT 0;
DECLARE total INT DEFAULT 0;
SET num_week = ABS(numday DIV 5);
SET num_day = MOD(numday, 5);

IF  numday > 0 and (WEEKDAY(DATE_ADD(mydate, INTERVAL num_day DAY)) < WEEKDAY(mydate)) THEN
    SET adj = 2;
END IF;

IF  numday < 0 and (WEEKDAY(DATE_ADD(mydate, INTERVAL num_day DAY)) > WEEKDAY(mydate)) THEN
    SET adj = 2;
END IF;

SET total = (num_week * 7 + adj + ABS(num_day));

IF numday < 0 THEN
    SET total = total * -1;
END IF;

RETURN DATE_ADD(mydate, INTERVAL total DAY); 

END

大家好,需要一个可以前后移动的公式。我使用了上面的一个,但发现了一个错误。我现在已经更正了,公式运行良好。

于 2017-11-13T11:14:25.630 回答
0
FUNCTION `Addworkday`(workday int(5),dt date) RETURNS date        
begin
    declare count int;        
    declare i int;        
    declare y date;        
set count=0;       
while count<workday do        
    set count=count+1;        
    set dt=date_add(dt,interval 1 day);        
    while dayofweek(dt) in (1,7) do        
        set dt=date_add(dt,interval 1 day);        
    end while;        
end while;        
return dt;        
end
于 2019-09-25T08:29:48.377 回答