SQL - Calculate hours from DateTime


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 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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Assuming start and end times are on the same day it is pretty easy

Select DateDiff(hour, startTime, endTime)


If you can span different days, you need to do a little owrk in your query to account for tis correctly
NerishaBAuthor Commented:
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.
If first ClockIn for a person is StartTime, and last Clockin for a person is EndTime (during a single day), your query will look like this:

	Emp.Name + ' '+ Emp.Surname As FullName,
	MIN(RC.ClockIn) as StartWorkTime,
	MAX(RC.ClockIn) as EndWorkTime,
	cast(datediff(mi, MIN(RC.ClockIn), MAX(RC.ClockIn)) as numeric(12,4)) / 60.0 as ShiftLenghtHours
from Employees Emp, ClockIns RC, InOrOut IO 
	Emp.Emp_Id = RC.Emp_ID 
	AND convert(varchar(10), RC.ClockIn, 111) >= '2010/10/20' 
	AND convert(varchar(10), RC.ClockIn, 111) <= '2010/10/27'
group by Emp.Name + ' '+ Emp.Surname,
	DATEPART(yyyy, RC.ClockIn),
	DATEPART(mm, RC.ClockIn),
	DATEPART(dd, RC.ClockIn)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SharathData EngineerCommented:
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?
NerishaBAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.