NerishaB
asked on
SQL - Calculate hours from DateTime
Hi,
I have the following SQL query:
Select DISTINCT Emp.Name + ' '+ Emp.Surname As FullName, RC.ClockIn,
IO.Name, Present = CASE Dir.InOrOut WHEN 'IN' THEN 'Y' WHEN 'OUT' THEN 'N/A' END
FROM Employees Emp, ClockIns RC, InOrOut IO
WHERE Emp.Emp_Id = RC.Emp_ID
AND IO.IO_Id = RC.IO_ID
AND convert(varchar(10), RC.ClockIn, 111) >= '2010/10/20'
AND convert(varchar(10), RC.ClockIn, 111) <= '2010/10/27'
The result is displayed as follows:
FullName ClockIn Name Present
Person1 2010-01-05 08:30:30 IN Y
Person1 2010-01-05 10:00:00 IN Y
Person1 2010-01-05 11:23:21 IN Y
Person1 2010-01-05 17:00:00 OUT N/A
Person2 2010-01-05 07:58:10 IN Y
Person2 2010-01-05 12:15:48 IN Y
Person3 2010-01-05 16:50:00 OUT N/A
I want to write a query to calculate how many hours a person has been at work. As you can see with the results, I need to find the first time the person logged in, and then find the las time the person logged out, and calculate the difference in time.
any ideas on how to do this?
I have the following SQL query:
Select DISTINCT Emp.Name + ' '+ Emp.Surname As FullName, RC.ClockIn,
IO.Name, Present = CASE Dir.InOrOut WHEN 'IN' THEN 'Y' WHEN 'OUT' THEN 'N/A' END
FROM Employees Emp, ClockIns RC, InOrOut IO
WHERE Emp.Emp_Id = RC.Emp_ID
AND IO.IO_Id = RC.IO_ID
AND convert(varchar(10), RC.ClockIn, 111) >= '2010/10/20'
AND convert(varchar(10), RC.ClockIn, 111) <= '2010/10/27'
The result is displayed as follows:
FullName ClockIn Name Present
Person1 2010-01-05 08:30:30 IN Y
Person1 2010-01-05 10:00:00 IN Y
Person1 2010-01-05 11:23:21 IN Y
Person1 2010-01-05 17:00:00 OUT N/A
Person2 2010-01-05 07:58:10 IN Y
Person2 2010-01-05 12:15:48 IN Y
Person3 2010-01-05 16:50:00 OUT N/A
I want to write a query to calculate how many hours a person has been at work. As you can see with the results, I need to find the first time the person logged in, and then find the las time the person logged out, and calculate the difference in time.
any ideas on how to do this?
ASKER
But there is a problem here, I dont know the "Start" and "End" date. Both the Time in and Time Out are stored in the same field. I need to somehow find out the first clockIn for the day, and the last clock out to become my StartTime and EndTime. This will be for the same day.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Do you have more than one "OUT" records for a person?
How do you want your expected result to be displayed?
Person2 does not have OUT time, what would be the no. of hours worked for this person?
Person3 does not have IN time, Is this valid? what would be the no. of hours worked for this person?
ASKER
Sometimes a person logs in and forgets to log out, in this case, we would like to use a default Start Time and end Time. The result that RGBDart works fine for now. Thanks
Select DateDiff(hour, startTime, endTime)
http://msdn.microsoft.com/en-us/library/aa258269(v=sql.80).aspx
If you can span different days, you need to do a little owrk in your query to account for tis correctly