-1

I have two date values: 10:30:00 and 15:59:00.

How do I get the difference in hours and minutes in a query in SQL Server 2008? to give 05:29

and convert it to decimal to give 5.29

mohamed
  • 1
  • 1
  • 4

4 Answers4

1

I would get the minutes and then handle the rest using arithmetic:

select (datediff(minute, val1, val2) / 60) +
       (datediff(minute, val1, val2) % 60 / 100.0)
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

try this

declare @date1 datetime
declare @date2 datetime

select @date1 = '00:25:10';
select @date2 = '23:05:10';

select cast(DATEDIFF(minute, @date1, @date2) / 60.0 as decimal(10,2))
reza.cse08
  • 5,938
  • 48
  • 39
0
CREATE Function [dbo].[udf_Elapsed] (@Date1 DateTime,@Date2 DateTime)

Returns Varchar(150)
As

Begin
    Declare @sec int,@hour int,@minut int,@days int 
    SET @sec = DATEDIFF (SECOND,@Date1,@Date2)
    Set @days = @sec / 86400
    Set @sec = @sec - (@days * 86400)
    Set @hour = @sec / 3600
    Set @sec = @sec - (@hour * 3600)
    Set @minut = @sec / 60
    Set @sec = @sec - (@minut * 60)
    Return right(concat('000',@days),3)+':'+right(concat('00',@hour),2)+':'+right(concat('00',@minut),2)+':'+right(concat('00',@sec),2)
End


Select [dbo].[udf_Elapsed] ('2016-05-05 10:30','2016-05-05 15:59')

returns 000:05:29:00 (days:hh:mm:ss)

Abdul Rasheed
  • 6,486
  • 4
  • 32
  • 48
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
0

Use DateDiff function like this to get result. enter image description here

Shahzad Riaz
  • 354
  • 1
  • 6