我需要计算差异并让我的答案显示分钟和秒。这是样本
Declare @clockin datetime, @clockout datetime, @total decimal(18,4)
Set @clockin = '2015-01-03 08:15:19.000'
Set @clockout = '2015-01-03 12:02:42.000'
Set @total = DateDiff(minute, @clockin, @clockout)
但这会返回一个整数,我需要它来显示秒数。
答案 0 :(得分:1)
我希望这种方法可以帮助你,这种解决方案很简单,我们只需要在两者之间获得秒数,然后我们通过一个简单的除法计算分钟数,然后用模块运算符计算秒数。
Declare @clockin datetime, @clockout datetime, @totalSeconds int
Set @clockin = '2015-01-03 08:15:19.000'
Set @clockout = '2015-01-03 12:02:42.000'
Select @totalSeconds = Cast((@clockout - @clockin) as Float) * 24.0 * 60 *60
Select @totalSeconds /60 as [Minutes], @totalSeconds % 60 as [Seconds]
如果你想扩展你的方法,只需稍微改变一下,以秒为单位计算差异,然后按照上面的方式进行相同的操作来分隔分钟和秒,它看起来像这样:
Declare @clockin datetime, @clockout datetime, @total int
Set @clockin = '2015-01-03 08:15:19.000'
Set @clockout = '2015-01-03 12:02:42.000'
Set @total = DateDiff(second, @clockin, @clockout)
Select @total/60 as [Minutes], @total % 60 as [Seconds]
结果:
会议纪要:227,秒:23
答案 1 :(得分:1)
按照您的意愿玩这个,如果你只悬停在返回和int
上,真正的问题是DATEDIFFDeclare @clockin datetime, @clockout datetime, @total datetime
declare @realtotal decimal(18,4)
Set @clockin = '2015-01-03 08:15:19.000'
Set @clockout = '2015-01-03 12:02:42.000'
select @total = dateadd(ss, DATEDIFF(ss, @clockin, @clockout), cast(0 as DateTime))
select @total
select @realtotal = datepart(hour,@total)*60 + datepart(minute,@total) + (datepart(second,@total)*.01)
select @realtotal