0

I have this query that shows how many hours, minutes and seconds a worker has done for a day:

SELECT 
   Employee_Number, CAST([DateTime] as Date) as 'DateTime', 
   MIN([DateTime]) as 'MIN', MAX([DateTime]) as 'MAX', 
   [Hours Worked] = CAST((DATEDIFF(HOUR , min([DateTime]),  
   max([DateTime])) % 24 ) AS VARCHAR)  +':'+ CAST((DATEDIFF(MINUTE, min([DateTime]), 
   max([DateTime])) % 60) AS VARCHAR) + ':' + CAST((DATEDIFF(SECOND, min([DateTime]), 
   max([DateTime])) % 60) AS VARCHAR) + '0' from tblExtract group by Employee_Number, 
   Cast([DateTime] as Date)

My problem is I want to convert the Hours worked column in hh:mm:ss. How can I resolve it?

2
  • More information about your question please, what language are you using? why do you want your date in hours? if you explain better you will get more and better answers Commented May 29, 2014 at 6:52
  • @GaryPerry what I want is to convert the values of Hours Worked column to this format hh:mm:ss. I am using sql server 2012 Commented May 29, 2014 at 6:59

4 Answers 4

1
CONVERT(NVARCHAR, MAX([DateTime]) - MIN(DateTime]), 108) AS [Hours Works]
Sign up to request clarification or add additional context in comments.

Comments

1

Try to use this at the palce of hours_worked:-

[Hours Worked] = CONVERT(VARCHAR(8),DateTime,108)

Hope this will be helpful to you.

Comments

1

Just use the Convert() function using the pattern for hh:mm:ss like so:

CONVERT(VARCHAR(8),DateTime,108)

Other patterns for convert are listed here.

Comments

1

use like this

[Hours Worked] = CONVERT(VARCHAR(15),CAST((DATEDIFF(HOUR , min([DateTime]),108)

or simply

[Hours Worked] = CONVERT(VARCHAR(15),min([DateTime]),108)

Here are the different formats

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.