欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

计算经历的时间

程序员文章站 2022-09-04 13:03:07
参考实例: --定义变量 DECLARE @startTime DATETIME DECLARE @endTime DATETIME DECLARE @HOUR INT DECLARE @MINUTE INT DECLARE @SECOND INT DECLARE @Minutes VARCHAR ......

参考实例:

 

--定义变量
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'秒 '