参考实例:
--定义变量 DECLARE @startTime DATETIME DECLARE @endTime DATETIME DECLARE @HOUR INT DECLARE @MINUTE INT DECLARE @SECOND INT DECLARE @Minutes VARCHAR --为变量赋值 SET @startTime = '2020-02-10 08:30:00' SET @endTime = '2020-02-19 17:10:09' SET @HOUR = DATEDIFF(HOUR, @startTime, @endTime) SET @MINUTE = DATEDIFF(MINUTE, @startTime, @endTime) SET @SECOND = DATEDIFF(SECOND, @startTime, @endTime) SET @Minutes = CASE WHEN DATEPART(SECOND, @endTime) >= DATEPART(SECOND, @startTime) THEN CAST((@MINUTE % 60) AS VARCHAR) ELSE CAST((DATEDIFF(MINUTE, DATEADD(MINUTE, -1, @endTime), @endTime) % 60) AS VARCHAR) END --SELECT SELECT [DD:HH:MM:SS] = CAST((@HOUR / 24) AS VARCHAR) +':'+ CAST((@HOUR % 24) AS VARCHAR) +':'+ @Minutes + ':' + CAST((@SECOND % 60) AS VARCHAR), [字符串格式] = CAST((@HOUR / 24) AS VARCHAR) +N'天 '+ CAST((@HOUR % 24) AS VARCHAR) +N'小时 ' + @Minutes + N'分钟 ' + CAST((@SECOND % 60) AS VARCHAR) + N'秒 'Source Code