Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have problem with calculating the total working time in h:m:s format

pls help.

this is my sample data

empid time        date         status
1     9:00:04     08-01-2013   login
1     9:30:45     08-01-2013   logOut
1     9:45:20     08-01-2013   login
1     12:30:59    08-01-2013   logOut
Posted
Updated 7-Jan-13 15:29pm
v2

Hi Manoj

As per my understanding you want this .....

I create a table like you mention(add a primary key ) and fill the same data .....

SQL
Create table T1 
(ID int identity (1,1),
 EmpID int , 
 Etime time, 
 EDate date, 
 EStatus Char(1) )

Write a sql query
SQL
SELECT Tab1.EMPID , Tab1.EDate
,SUM([SECONDS]) AS [TOTAL TIME in SECONDS]
,CONVERT(varchar (20), SUM(Tab1.[SECONDS])/(60 * 60))+ ':' +
CONVERT(varchar(20), (SUM(Tab1.[SECONDS])%(60 * 60))/ 60) + ':' +
CONVERT(varchar(20), (SUM(Tab1.[SECONDS])%(60 * 60*60))%60) AS [WORKING TIME]
FROM
(SELECT A.EmpID , A.Etime AS [IN TIME]
, B.Etime AS [OUT TIME]
, A.EDate
, Datediff (SECOND,A.Etime,B.Etime  ) as [SECONDS]
FROM
T1 as A (NOLOCK)
INNER JOIN t1 as B  ON a.ID = (b.ID -1)  AND A.EStatus = 'I'
  WHERE  B.EStatus = 'O' ) AS Tab1
  GROUP BY Tab1.EMPID , Tab1.EDate


Output
CSS
EMPID       EDate      TOTAL TIME in SECONDS WORKING TIME
----------- ---------- --------------------- --------------------------------------
1           2013-08-01  11780                 3:16:20


----------------------------------------------------------------------------------

Regards,
Vijay
 
Share this answer
 
Comments
manoj12.shrivastava 8-Jan-13 10:54am    
Thank you very much sir
What have you tried ? DateDiff will give you the difference between two dates.
 
Share this answer
 
you can refer version 2, in below link

Get duration between two dates in years,months,days and hours using sql server[^]

Happy Coding!
:)
 
Share this answer
 
Comments
URVISH_SUTHAR1 8-Jan-13 3:27am    
yep, it can fulfill your recruitment

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