VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 数据库 > SQL教程 >
  • 计算经历的时间

参考实例:

 

复制代码
--定义变量
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''
复制代码

 


相关教程