TIMEDIFF()

Description

TIMEDIFF() returns expr1 − expr2 expressed as a time value.

The result returned by TIMEDIFF() is limited to the range allowed for TIME values. Alternatively, you can use either of the functions TIMESTAMPDIFF() and UNIX_TIMESTAMP(), both of which return integers.

Syntax

  1. > TIMEDIFF(expr1,expr2)

Arguments

ArgumentsDescription
expr1,expr2Required. expr1 and expr2 are strings which are converted to TIME or DATETIME expressions; these must be of the same type following conversion. Returns NULL if expr1 or expr2 is NULL.

Examples

  1. mysql> select timediff("22:22:22", "11:00:00");
  2. +------------------------------+
  3. | timediff(22:22:22, 11:00:00) |
  4. +------------------------------+
  5. | 11:22:22.000000 |
  6. +------------------------------+
  7. 1 row in set (0.01 sec)
  8. mysql> select timediff(cast('22:22:22' as time), null);
  9. +--------------------------------------------+
  10. | timediff(cast(22:22:22 as time(26)), null) |
  11. +--------------------------------------------+
  12. | NULL |
  13. +--------------------------------------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select timediff(CAST('2017-08-08 22:22:22' as datetime), CAST('2000-01-02 11:00:00' as datetime));
  16. +------------------------------------------------------------------------------------------------+
  17. | timediff(cast(2017-08-08 22:22:22 as datetime(26)), cast(2000-01-02 11:00:00 as datetime(26))) |
  18. +------------------------------------------------------------------------------------------------+
  19. | 154283:22:22 |
  20. +------------------------------------------------------------------------------------------------+
  21. 1 row in set (0.00 sec)
  1. create table time_04 (t1 int,t2 time,t3 datetime,t4 timestamp);
  2. insert into time_04 values (1,"344:59:09","2020-09-12","2021-09-22 10:01:23.903");
  3. mysql> select * from time_04;
  4. +------+-----------+---------------------+---------------------+
  5. | t1 | t2 | t3 | t4 |
  6. +------+-----------+---------------------+---------------------+
  7. | 1 | 344:59:09 | 2020-09-12 00:00:00 | 2021-09-22 10:01:24 |
  8. +------+-----------+---------------------+---------------------+
  9. 1 row in set (0.00 sec)