Solved

get hours worked between two datetime fields

Posted on 2016-09-26
5
88 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
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 28

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

778 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