Difference between two dates with time greater than 24 hours?

Asked

Viewed 58 times

4

I have the following query:

Sqlfiddle

Past 24 hours, it Zera the time and starts counting again.

I need to draw the difference between the hours, as long as they have to pass 24 hours.

For example, in the first row of the result I have 2018-09-03T10:35:16Z. Picking the difference between today (2018-09-05T14:27:39Z) and that day, would give the following result:

51:55:28 which is the same thing as 2 days, 3 hours, 55 minutes and 28 seconds

But the result is being only 3:55:28

How to make this date difference exceed 24 hours?

2 answers

4


It is not an optimal solution, but you can take only the minutes and seconds that are already calculated and concatenate with a TIMESTAMPDIFF in hours.

Sqlfiddle

SQL:

SELECT 
    date_ini,
    NOW(),
    CONCAT(
        TIMESTAMPDIFF(HOUR, date_ini, NOW()),
        TIME_FORMAT(
            TIMESTAMP(SEC_TO_TIME(TIMESTAMPDIFF(SECOND, date_ini, NOW()))),
            ':%i:%s'
        )
    ) as "Tempo de Análise"
from TEMPO;
  • OMG! Helped me so much. At least you solved what I needed. Thank you so much. =)

2

Only as a response complement I use as follows:

SELECT DATEINI, NOW(), 
CONCAT(TRUNCATE(TIMESTAMPDIFF(MINUTE, DATEINI, NOW())/60,0), 
':',TIMESTAMPDIFF(MINUTE, DATEINI, NOW())%60 ,
':', TIMESTAMPDIFF(second, DATEINI, NOW())%60 ) 
AS 'Tempo de Análise'
FROM TEMPO
  • In this case, it worked as well, but Fernando Savio’s response was "correct". Example: 49:36:05 and his answer, I don’t know why, cut the zero: 49:36:5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.