1、mysql
CREATE DEFINER=`dtssyncwriter`@`%` FUNCTION `TIMER_MIN`(
startDay datetime,endDay datetime) RETURNS varchar(255) CHARSET utf8
SQL SECURITY INVOKER
BEGIN
DECLARE str VARCHAR(255);
IF (DATE_FORMAT(endDay,'%H%i%s') >= DATE_FORMAT(startDay,'%H%i%s') && DATE_FORMAT(endDay,'%Y%m%d') >= DATE_FORMAT(startDay,'%Y%m%d'))
THEN SET str = CONCAT(
CASE WHEN DATEDIFF(endDay, startDay) != 0
THEN CONCAT(DATEDIFF(endDay, startDay),'天 ') ELSE '' END,
TIME_FORMAT(
TIMEDIFF(
DATE_ADD(
endDay,
INTERVAL - DATEDIFF(endDay, startDay) DAY
),
startDay
),
'%H:%i'
)
);
ELSEIF (DATE_FORMAT(endDay,'%H%i%s') < DATE_FORMAT(startDay,'%H%i%s') && DATE_FORMAT(endDay,'%Y%m%d') >= DATE_FORMAT(startDay,'%Y%m%d'))
THEN SET str = CONCAT(
CASE WHEN DATEDIFF(endDay, startDay) = 1
THEN TIME_FORMAT(
-TIMEDIFF(startDay, endDay),
'%H:%i'
)
WHEN DATEDIFF(endDay, startDay) = 0
THEN TIME_FORMAT(
TIMEDIFF(endDay, startDay),
'%H:%i'
)
ELSE CONCAT(
DATEDIFF(endDay, startDay)-1,'天 ',
TIME_FORMAT(
TIMEDIFF(
DATE_ADD(
endDay,
INTERVAL - DATEDIFF(endDay, startDay) + 1 DAY
),
startDay
),
'%H:%i'
)
)
END
);
ELSEIF (DATE_FORMAT(endDay,'%H%i%s') >= DATE_FORMAT(startDay,'%H%i%s') && DATE_FORMAT(endDay,'%Y%m%d') < DATE_FORMAT(startDay,'%Y%m%d'))
THEN SET str =
CASE WHEN (DATEDIFF(endDay, startDay) + 1) = 0
THEN TIME_FORMAT(
TIMEDIFF( endDay, startDay),
'%H:%i'
)
ELSE CONCAT((DATEDIFF(endDay, startDay) + 1),'天 ',
TIME_FORMAT(
TIMEDIFF(
DATE_ADD(
startDay,
INTERVAL - DATEDIFF(startDay,endDay ) +1 DAY
),
endDay
),
'%H:%i'
)
)
END;
ELSE SET str = CONCAT(
CASE WHEN DATEDIFF(endDay, startDay) != 1
THEN CONCAT(DATEDIFF(endDay, startDay),'天 ') ELSE '' END,
TIME_FORMAT(
TIMEDIFF(
DATE_ADD(
startDay,
INTERVAL - DATEDIFF(startDay, endDay) DAY
),
endDay
),
'%H:%i'
)
);
end IF;
RETURN str;
END
2、sqlserver简易版
ALTER FUNCTION [dbo].[jn_timer_min]
( @startDay AS DATETIME ,
@endDay AS DATETIME
)
RETURNS VarChar(255)
AS
BEGIN
DECLARE @str VarChar(255);
IF (@endDay > @startDay and ( DATEDIFF( ss, @startDay, @endDay ) / ( 60 * 60 * 24 ) ) > 0)
SET @str = (
CAST ( CAST ( DATEDIFF( ss, @startDay, @endDay ) / ( 60 * 60 * 24 ) AS INT ) AS VarChar ) + '天 '
+ CAST ( CAST ( DATEDIFF( ss, @startDay, @endDay ) % 86400 / 3600 AS INT ) AS VarChar ) + ':'
+ CAST ( CAST ( DATEDIFF( ss, @startDay, @endDay ) % 3600 / 60 AS INT ) AS VarChar )) ;
IF (@endDay > @startDay and ( DATEDIFF( ss, @startDay, @endDay ) / ( 60 * 60 * 24 ) ) = 0)
SET @str = (
CAST ( CAST ( DATEDIFF( ss, @startDay, @endDay ) % 86400 / 3600 AS INT ) AS VarChar ) + ':'
+ CAST ( CAST ( DATEDIFF( ss, @startDay, @endDay ) % 3600 / 60 AS INT ) AS VarChar )) ;
IF (@endDay < @startDay and ( DATEDIFF( ss, @endDay, @startDay ) / ( 60 * 60 * 24 ) ) > 0)
SET @str = ( '-'
+ CAST ( CAST ( DATEDIFF( ss, @endDay, @startDay ) / ( 60 * 60 * 24 ) AS INT ) AS VarChar ) + '天 '
+ CAST ( CAST ( DATEDIFF( ss, @endDay, @startDay ) % 86400 / 3600 AS INT ) AS VarChar ) + ':'
+ CAST ( CAST ( DATEDIFF( ss, @endDay, @startDay ) % 3600 / 60 AS INT ) AS VarChar )) ;
IF (@endDay < @startDay and ( DATEDIFF( ss, @endDay, @startDay ) / ( 60 * 60 * 24 ) ) = 0)
SET @str = ( '-'
+ CAST ( CAST ( DATEDIFF( ss, @endDay, @startDay ) % 86400 / 3600 AS INT ) AS VarChar ) + ':'
+ CAST ( CAST ( DATEDIFF( ss, @endDay, @startDay ) % 3600 / 60 AS INT ) AS VarChar )) ;
RETURN @str;
END