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
Solved

get hours worked between two datetime fields

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

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

792 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