该函数在MYSQL下编写,5.1版本测试通过。 函数需要传入三个参数,类型(用于区别应返回时差年、时差月...时差秒),起始时间(用于计算),结束时间(用于计算) 无 DELIMITER $$CREATE FUNCTION DateBetween (AType INT, ADateStart DATETIME, ADateEnd DATETIME)R
该函数在MYSQL下编写,5.1版本测试通过。DELIMITER $$ CREATE FUNCTION DateBetween (AType INT, ADateStart DATETIME, ADateEnd DATETIME) RETURNS BIGINT BEGIN DECLARE AResult BIGINT DEFAULT -1; /*入参AType INT类型,结果类型 1:年 2:月 3:日 4:小时 5:分钟 6:秒钟 (必须在1-6内,否则结果返回-1) *入参ADateStart DATETIME类型,起始时间 (必须是一个正确的日期参数) *入参ADateEnd DATETIME类型,结束时间 (必须是一个正确的日期参数) *返回值 BIGINT类型,返回起始之间与结束时间的指定时差 (成功返回计算出的时差,失败返回-1) */ IF ADateStart != '' AND ADateEnd != '' AND AType >=1 AND AType <= 6 THEN CASE AType WHEN 1 THEN SELECT TIMESTAMPDIFF(YEAR, ADateStart, ADateEnd) INTO AResult; WHEN 2 THEN SELECT TIMESTAMPDIFF(MONTH, ADateStart, ADateEnd) INTO AResult; WHEN 3 THEN SELECT TIMESTAMPDIFF(DAY, ADateStart, ADateEnd) INTO AResult; WHEN 4 THEN SELECT TIMESTAMPDIFF(HOUR, ADateStart, ADateEnd) INTO AResult; WHEN 5 THEN SELECT TIMESTAMPDIFF(MINUTE, ADateStart, ADateEnd) INTO AResult; WHEN 6 THEN SELECT TIMESTAMPDIFF(SECOND, ADateStart, ADateEnd) INTO AResult; END CASE; END IF; RETURN(AResult); END$$
SELECT DateBetween(0, '2010-01-01 00:00:00', '2010-01-01 10:00:00'); SELECT DateBetween(1, '2001-01-01', '2010-01-01'); SELECT DateBetween(2, '2010-01-01', '2010-12-01'); SELECT DateBetween(3, '2010-01-01', '2010-01-31'); SELECT DateBetween(4, '2010-01-01 00:00:00', '2010-01-01 10:00:00'); SELECT DateBetween(5, '2010-01-01 00:00:00', '2010-01-01 10:00:00'); SELECT DateBetween(6, '2010-01-01 00:00:00', '2010-01-01 10:00:00'); SELECT DateBetween(7, '2010-01-01 00:00:00', '2010-01-01 10:00:00');
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com