• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 33
  • Last Modified:

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)
0
Danean
Asked:
Danean
  • 3
1 Solution
 
_agx_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
 
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.

thanks.
0
 
NorieVBA ExpertCommented:
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
 
_agx_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

Open in new window

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

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
FROM	@table t CROSS APPLY
		( 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.
1
 
_agx_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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now