mysql计算工作日函数_SQL实现工作日计算_MySQL

bitsCN.com

CREATE FUNCTION Time_Span_of_minutes(

start_day DATETIME,

end_day DATETIME)

RETURNS FLOAT

BEGIN

-- 返回按分钟计算两段时间的间隔,采用逐日靠近的方法求解,一天按8小时480分钟计算,周末不计,不考虑法定节假日。

-- 如果起始日期在周末,则计算当天时间。

DECLARE minutes FLOAT;

DECLARE next_day DATETIME;

SET minutes=0.0;

SET next_day=start_day;

IF(TIMESTAMPDIFF(DAY,start_day,end_day)<1.0) THEN

SET minutes=minutes+TIMESTAMPDIFF(MINUTE,start_day,end_day);

ELSE

WHILE TIMESTAMPDIFF(DAY,next_day,end_day)>=1.0 DO

IF ((DAYOFWEEK(next_day)=7) OR (DAYOFWEEK(next_day)=1)) THEN

SET next_day=next_day+INTERVAL 1 DAY;

ELSE

SET next_day=next_day+INTERVAL 1 DAY;

SET minutes=minutes+480.0;

END IF;

END WHILE;

SET minutes=minutes+TIME

你可能感兴趣的:(mysql计算工作日函数)