Solved

get hours worked between two datetime fields

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

table
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?
0
Comment
Question by:Colin Hart
[X]
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
5 Comments
 
LVL 69

Accepted Solution

by:
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'

SELECT CAST(SUM(DATEDIFF(MINUTE,
    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
0
 
LVL 29

Assisted Solution

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

--


CREATE TABLE testDt
(
	 EmployeeID INT              
	,StartDateTime DATETIME                
	,EndDateTime DATETIME
)
GO

INSERT INTO testDt VALUES
(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')
GO


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 
			CAST((DATEDIFF(MINUTE, 
			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 
		CAST((DATEDIFF(MINUTE, 
			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				
FROM
(
	SELECT EmployeeID , StartDateTime , EndDateTime , DATEDIFF(dd, 0,@Date) + CONVERT(DATETIME,@StartTime) st , DATEDIFF(dd, 0,@Date) + CONVERT(DATETIME,@EndTime) et
	FROM testDt t
)r

--

Open in new window


Ouptut....

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

:)
0
 

Author Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 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