get hours worked between two datetime fields

Posted on 2016-09-26
Last Modified: 2016-11-01
I have a table of hours worked and I need to calculate the time worked between two periods

EmployeeID               StartDateTime                 EndDateTime
100                               09/01/2016 08:00:00    09/01/2016 16:45:00
101                               09/01/2016 15:15:00    09/02/2016 18:00:00
102                               09/01/2016 11:00:00    09/01/2016 18:45:00

I need to write a query that will pull back the number of hours worked for all employees between say
on 9/1/16 between the times of 10am to 1pm
so that would be a total of 5 hours
emp 100 has 3 hours
emp 101 has 0 hours
emp 102 has 2 hours

who would I write that?
Question by:Colin Hart
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 69

Accepted Solution

Scott Pletcher earned 250 total points
ID: 41816966
Something like this:

DECLARE @StartDateTime datetime
DECLARE @EndDateTime datetime

SET @StartDateTime = '20160901 10:00'
SET @EndDateTime = '20160901 13:00'

    CASE WHEN StartDateTime < @StartDateTime THEN @StartDateTime ELSE StartDateTime END,
    CASE WHEN EndDateTime > @EndDateTime THEN @EndDateTime ELSE EndDateTime END))
        / 60.0 AS decimal(9, 2)) AS Total_Hours_Worked
FROM table_name
WHERE StartDateTime <= @EndDateTime AND EndDateTime > @StartDateTime
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 250 total points
ID: 41817119
Complete code..Enjoy !!


	 EmployeeID INT              
	,StartDateTime DATETIME                
	,EndDateTime DATETIME

(100                               ,'09/01/2016 08:00:00'    ,'09/01/2016 16:45:00'),
(101                               ,'09/01/2016 15:15:00'    ,'09/02/2016 18:00:00'),
(102                               ,'09/01/2016 11:00:00'    ,'09/01/2016 18:45:00')

DECLARE @Date AS DATE = '2016-09-01'
DECLARE @StartTime AS TIME = '10:00'
DECLARE @EndTime AS TIME = '13:00'

SELECT CONCAT(  'Emp ', EmployeeID , ' has '  ,
			CASE WHEN StartDateTime < st THEN st ELSE StartDateTime END,
			CASE WHEN EndDateTime > et THEN et ELSE EndDateTime END))
        / 60.0 AS INT) <= 0 THEN 0 ELSE 
			CASE WHEN StartDateTime < st THEN st ELSE StartDateTime END,
			CASE WHEN EndDateTime > et THEN et ELSE EndDateTime END))
        / 60.0 AS INT) END , ' Hours' ) Output				
	SELECT EmployeeID , StartDateTime , EndDateTime , DATEDIFF(dd, 0,@Date) + CONVERT(DATETIME,@StartTime) st , DATEDIFF(dd, 0,@Date) + CONVERT(DATETIME,@EndTime) et
	FROM testDt t


Open in new window


Emp 100 has 3 Hours
Emp 101 has 0 Hours
Emp 102 has 2 Hours


Author Comment

by:Colin Hart
ID: 41860369
Sorry for the delay, I honestly thought I had put in my comments a while back.

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GeoClustering  and AOG 25 43
MS SQL Sever Import/export problem 7 50
Delete old Sharepoint backups 2 22
Need age at date of document 5 20
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question