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.

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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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.
DaneanAuthor 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.

NorieAnalyst 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)
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.


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

Open in new window


CREATE FUNCTION GetTimeDifferenceString
	@startDate datetime, @endDate datetime
       DECLARE @secondsDiff BIGINT

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

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

Open in new window

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
		( SELECT dateDiff(s, t.StartDate, t.lockTime)) AS calc(SecondsDiff)

Open in new window

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial

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

CREATE FUNCTION GetTimeDifferenceString

Typo correction for the archives.  Obviously the function names should've matched ;-)
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

From novice to tech pro — start learning today.