# help with datediff function formatting and getting the correct time difference in hh:mm:ss format

I have a SQL table with StartDate and LockTime columns in datetime format.

I want to do an update query to a calculated column in the same table called TimeDifference in Hour, Min, Sec format.

UPDATE TEST
SET  TimeDifference = LockTime - StartDate

Sometimes the hour differences can be greater than 24 and 48 hours.  For example, 52:20:10  (52 hours, 20 minutes and 10 seconds)
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Which DBMS?

Also, why not calculate on the front end? That way if the requirements ever change, you won't have to update the entire table.
0
Author Commented:
I am using sql server 2018.  i don't know about doing the calculating up front, so adding a update query seemed to be the easiest way for me to do it.

thanks.
0
Analyst Assistant Commented:
Don't know if it helps but you can use something like this to get the duration in H:M:S.

RIGHT('0' + CAST(DATEDIFF(SECOND, StartDate, LockTime)/ 3600 AS VARCHAR),2) + ':' + RIGHT('0' + CAST((DATEDIFF(SECOND, StartDate, LockTime)/ 60) % 60 AS VARCHAR),2) + ':' + RIGHT('0' + CAST(DATEDIFF(SECOND, StartDate, LockTime)% 60 AS VARCHAR),2)
0
Commented:
If it were me, I wouldn't store the formatted string, but calculate it on the fly when needed.  If you really need to do this in SQL, you could create a scalar function, so the logic can be changed easily and reused anywhere.

Usage:

``````SELECT StartDate, LockTime, dbo.GetTimeElapsed(StartDate, LockTime) AS TimeElapsedString
FROM   @table
``````
Function:

``````CREATE FUNCTION GetTimeDifferenceString
(
@startDate datetime, @endDate datetime
)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @secondsDiff BIGINT

SET @secondsDiff = dateDiff(s, @startDate, @endDate)

RETURN FORMAT(@secondsDiff / 60 / 60, '00')
+':'+ FORMAT(@secondsDiff / 60 % 60, '00')
+':'+ FORMAT(@secondsDiff  % 60 , '00')
END
GO
``````

Or if it's a one time deal, it could also be done inline

``````SELECT t.StartDate
, t.LockTime
,  FORMAT(SecondsDiff / 60 / 60, '00')
+':'+ FORMAT(SecondsDiff / 60 % 60, '00')
+':'+ FORMAT(SecondsDiff  % 60 , '00')  AS TimeElapsedString
FROM	@table t CROSS APPLY
( SELECT dateDiff(s, t.StartDate, t.lockTime)) AS calc(SecondsDiff)
``````

Though you could do the same calculations in any language, web app, etc.  Just calculate the total seconds difference and divide to get the various intervals (days, hours, minutes, etc...) like in the function above.
1

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:

Usage:
SELECT StartDate, LockTime, dbo.GetTimeElapsed(StartDate, LockTime) AS TimeElapsedString
FROM   @table

Function:
CREATE FUNCTION GetTimeDifferenceString
(
...

Typo correction for the archives.  Obviously the function names should've matched ;-)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.