views:

31

answers:

3

Hi I am trying to get the the result of in time and out time from dates but it returns only hours using following select Query as follows

SELECT DATEDIFF(Hh,InTime,OutTime)  as Diff_time from EmpLogTable

and i need result in HH:MM Suppose my in time is 11 am and out is 5.49pm so o/p would be 6.49 but using above select query i am getting o/p as 7 only if any body has a solution then please let me know

Thanking you in Advance Umesh Rakhe

A: 
SELECT Outime - InTime as Diff_time from EmpLogTable

DATEDIFF measures day, hour etc boundaries: you're asking for a true date/time difference. In this case, I'd simply subtract the values rather than using a complex nested DATEDIFF. Or do it in the client.

If you have a interval > 24 hours though, then you'd need a nested DATEDIFF to get 25 hours: my answer would give one day and one hour.

The hh:nn:ss format can be done via CONVERT with style 108, but again I'd do this in the client

gbn
That is not correct.datediff(mi, '2010-07-25 12:00:00', '2009-07-25 12:00:00') = -525600
el chief
@el chief: subtraction is not the same as DATEDIFF. What are you trying to say?
gbn
+1  A: 

The DATEDIFF function returns an INT so it will not work as you like, your best bet is to subtract InTime from OutTime or use DATEDIFF with minutes (n) instead.

Chris Diver
+1  A: 
  1. you should probably do UI formatting on the client, not the database

  2. you can use diff = datediff(mi, intime, outtime) to get the difference in minutes

then divide diff by 60 to get the hours

and take the modulus diff % 60 to get the remaining minutes

then turn into strings and you're good to go

el chief