Skip to content
Advertisement

Calculate Duration of Two time

Hi I am new to my sql I am trying to compute the duaration of two time

ex: timestart = 21:00 timeend = 03:00 result will be 06:00

my query is like this for compute but it gives me result of 18:00 I will really appreciate any help or addvice thank you

 TIME_FORMAT(ABS(SEC_TO_TIME(ABS(TIME_TO_SEC(timestart)-TIME_TO_SEC(timeend)))), '%H:%i')

Advertisement

Answer

Use case:

time_format(sec_to_time(case when timestart < timeend
                             then time_to_sec(timeend) - time_to_sec(timestart) 
                             else 24*60*60 + time_to_sec(timeend) - time_to_sec(timestart)
                        end), '%H:%i'
           )
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement