MySQL: TIMEDIFF Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL TIMEDIFF Function

In this guide, we will explain how to use the MySQL TIMEDIFF function with syntax and examples.

Description

The MySQL TIMEDIFF function returns the difference (expressed as a time value) between two time/datetime values.

Syntax

The syntax for the TIMEDIFF function is:

TIMEDIFF( time1, time2 )

Parameters or Arguments

time1 and time2

The two time/datetime to calculate the difference between. The calculation is time1 - time2.

Note

  • A time value can range from -838:59:59′ to ‘838:59:59’.
  • When using the TIMEDIFF function, both time1 and time2 must be both time values, or both datetime values. You can not have one be a time value and the other be a datetime value, or the TIMEDIFF function will return a NULL value.

Applies To

The TIMEDIFF function can be used in the following versions :

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1.1

Example

Let’s look at some TIMEDIFF function examples and explore how to use the TIMEDIFF function.

For example:

mysql> SELECT TIMEDIFF('2014-02-17 12:10:08', '2014-02-17 12:10:07');
Result: '00:00:01'

mysql> SELECT TIMEDIFF('12:10:08', '12:10:07');
Result: '00:00:01'

mysql> SELECT TIMEDIFF('11:41:14', '12:10:08');
Result: '-00:28:54'

mysql> SELECT TIMEDIFF('2014-02-17 12:10:08', '2014-02-16 12:10:08');
Result: '24:00:00'

mysql> SELECT TIMEDIFF('2014-02-17 12:10:08', '12:10:08');
Result: NULL

Next Topic : Click Here

This Post Has One Comment

Leave a Reply