SQL实现工作日计算_MySQL
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)
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+TIMESTAMPDIFF(MINUTE,next_day,end_day);
IF ((DAYOFWEEK(start_day)=7) OR (DAYOFWEEK(start_day)=1)) THEN
SET minutes=minutes+TIMESTAMPDIFF(MINUTE,start_day,CONVERT(CONCAT(SUBSTRING(start_day FROM 1 FOR 10),' 17:30:00'),DATETIME));
END IF;
END IF;
RETURN minutes;
END;
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)
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+TIMESTAMPDIFF(MINUTE,next_day,end_day);
IF ((DAYOFWEEK(start_day)=7) OR (DAYOFWEEK(start_day)=1)) THEN
SET minutes=minutes+TIMESTAMPDIFF(MINUTE,start_day,CONVERT(CONCAT(SUBSTRING(start_day FROM 1 FOR 10),' 17:30:00'),DATETIME));
END IF;
END IF;
RETURN minutes;
END;
没考虑中午休息时间
摘自 Jasper键盘舞步
bitsCN.com下一篇: php foreach用法
推荐阅读
-
【呕心总结】python如何与mysql实现交互及常用sql语句
-
用sql实现18位身份证校验代码分享 身份证校验位计算
-
ms sql server中实现的unix时间戳函数(含生成和格式化,可以和mysql兼容)
-
PHP+Mysql实现多关键字与多字段生成SQL语句的函数
-
asp实现计算两个时间内的工作日的函数
-
Oracle中实现MySQL show index from table命令SQL脚本分享
-
PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
-
MySQL 使用SQL语句修改表名的实现
-
sql语句如何实现插入不重复的20位字符串(mysql)?
-
SQL如何实现MYSQL的递归查询