troubleshooting Question

SQL query - calculating time taken from datetime fields

Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft SQL ServerSQL
6 Comments1 Solution460 ViewsLast Modified:
Hi Experts,

Bit stuck with my sql query:

I have a table of jobs, each job records a start-time and end-time but also has the option to record upto 3 pause-times and corresponding restart-times.

I need to establish from this the time taken per job and I began here:

SELECT     StartTime, TimeOut, dbo.ConvertSecondsToDDHHMMSS(DATEDIFF(ss, TimeIn, TimeOut)) AS TimeTaken,
                      dbo.ConvertSecondsToDDHHMMSS(DATEDIFF(ss, Pause1, Restart1)) AS Pause1, dbo.ConvertSecondsToDDHHMMSS(DATEDIFF(ss, Pause2, Restart2))
                      AS Pause2, dbo.ConvertSecondsToDDHHMMSS(DATEDIFF(ss, Pause3, Restart3)) AS Pause3
FROM         dbo.DPJobs

So far so good, my next step was going to be to Add the three Pause totals to get my total time paused per job and then finally subtract that from my TimeTaken.

THE PROBLEM - having used the Convert I can't sum my pause totals as they have become varchars...not sure how to get around this.

Any help greatly appreciated
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros