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_01(t1 time,t2 time,t3 time);
  2. insert into time_01 values("-838:59:59.0000","838:59:59.00","22:00:00");
  3. insert into time_01 values("0:00:00.0000","0","0:00");
  4. insert into time_01 values(null,NULL,null);
  5. insert into time_01 values("23","1122","-1122");
  6. insert into time_01 values("101412","4","-101219");
  7. insert into time_01 values("24:59:09.932823","24:02:00.93282332424","24:20:34.00000000");
  8. insert into time_01 values("2022-09-08 12:00:01","019","23403");
  9. mysql> select * from time_01;
  10. +------------+-----------+-----------+
  11. | t1 | t2 | t3 |
  12. +------------+-----------+-----------+
  13. | -838:59:59 | 838:59:59 | 22:00:00 |
  14. | 00:00:00 | 00:00:00 | 00:00:00 |
  15. | NULL | NULL | NULL |
  16. | 00:00:23 | 00:11:22 | -00:11:22 |
  17. | 10:14:12 | 00:00:04 | -10:12:19 |
  18. | 24:59:10 | 24:02:01 | 24:20:34 |
  19. | 12:00:01 | 00:00:19 | 02:34:03 |
  20. +------------+-----------+-----------+
  21. 7 rows in set (0.00 sec)
  22. mysql> select timediff(t1,t2) from time_01;
  23. +------------------+
  24. | timediff(t1, t2) |
  25. +------------------+
  26. | -1677:59:58 |
  27. | 00:00:00 |
  28. | NULL |
  29. | -00:10:59 |
  30. | 10:14:08 |
  31. | 00:57:09 |
  32. | 11:59:42 |
  33. +------------------+
  34. 7 rows in set (0.00 sec)