Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a report in which i have to calculate overall worked hours by any employee in a week. The report reflects hours more than 24 hours(or 86400 seconds). I want to convert all the seconds into "00:00:00" format keeping the format of the columns as datetime or time so that I can do further analysis using the time field.

This code gives me what I need however the above converts the seconds in INT to varchar which is not what I need for the report. I need the output to be in datetime/time format which can give me results like '40:00:00' for the given example.
I hope I am able I have provided all the information. Please help as I am struggling with this from many days.

What I have tried:

Below is what I have tried so far:
Declare @WorkTimeInSeconds int
Set @WorkTimeInSeconds = 144000 --This value is an example. Mostly values are above 86400 seconds
Select ActualHoursWorked =       convert(varchar,(@WorkTimeInSeconds)/3600)
            + ':' + right( '0' + convert(varchar,((@WorkTimeInSeconds) %3600)/60),2)
            + ':' + right( '0' + convert(varchar,(@WorkTimeInSeconds) %60),2)
Posted
Updated 19-Jan-17 22:37pm

1 solution

The problem is that 00:00:00 is not a date time format - it's a timespan. A date time value requires a date, and is limited to 23:59:99 as the time component as well, so you can't just say "90,909 seconds is a datetime of 25:15:09" because that isn't a valid datetime value.
I'd either return it as a NVARCHAR or a number of seconds and let the presentation software sort out exactly what it wants to display. (Or possibly as three separate INT values, but that's equally messy).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900