Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi! Good day. Im using ms sql server 2005 express and heres my table structure:


SQL
 USER |        Log_Date        |       Log_In_Time     |      Log_Out_Time     |
--------------------------------------------------------------------------------
 xxx  | 2013-10-02 23:00:31.623|2013-10-02 23:07:31.623|2013-10-03 01:08:31.623|


NOTE:

LOG_OUT_TIME is in other day which is October 3, 1:08 AM , LOG_IN_TIME is October 2, 11:00 PM

Now I want to add column: TOTAL

and it look like this:

SQL
|TOTAL|
-------
|2.08 |


Can anybody know the way?

Thank you in advance!
Posted

You can use DATEDIFF as
SQL
SELECT DATEDIFF(HOUR,Log_In_Time,Log_OUT_Time)

or
SQL
SELECT DATEDIFF(mi,Log_In_Time,Log_OUT_Time)/60;

Plese refer
http://technet.microsoft.com/en-us/library/ms189794%28v=sql.90%29.aspx[^]
 
Share this answer
 
v2
Try:
SQL
SELECT CAST(DATEDIFF(mi, Log_In_time, Log_Out_Time) AS FLOAT)/60 AS Total FROM MYTable

But do note - you sample doesn't generate the number you show!
 
Share this answer
 
SQL
Declare @Time int,@LoginTime datetime = '2013-10-02 23:00:00',@LogoutTime datetime = '2013-10-03 01:08:00'

Set @Time = DATEDIFF(Minute,@LoginTime,@LogoutTime)

SELECT CAST( @Time/60 AS VARCHAR(5))+ ' Hrs' + ':'+ RIGHT('0' + CAST( @Time%60 AS VARCHAR(2)), 2)+' Min' AS 'WorkingTime'
 
Share this answer
 
Try these, hope you will get ans,

SQL
select DATEDIFF(hh,getdate(),getdate()+1) --For hours
select DATEDIFF(mi,getdate(),getdate()+1) --for mins
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900