Search code examples
mysqltimedomo

Get the time difference in minutes between two timestamps (Mysql)


Hi I tried the following:

timediff(`date2`, `date1`) which gives me the time difference but if the days are different it does not account for the day difference. so if date2 is yesterday and date1 is today it will provide something like this: 00:00:07 I saw the timestampdiff function a couple of times, but I am using MySQL in Domo, and this function is not offered there. Any other suggestion how to get the time difference between two timestamps (where days are different) in minutes?


Solution

  • SELECT TIMESTAMPDIFF(MINUTE, '2020-01-07T12:17:03',  '2020-01-06T13:14:02')
    

    returns -1383

    you can change MINUTE to SECOND or other formats as well.

    https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff

    edit: sorry, just read that the function is not available in your environment...

    therefore I suggets to convert the datetimes to seconds and work your way up from there, e.g.

    SELECT (UNIX_TIMESTAMP('2020-01-07T12:17:03') - UNIX_TIMESTAMP('2020-01-06T13:14:02')) / 60