×

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples.

Introduction of MySQL DATEDIFF() function

DATEDIFF() function in MySQL is used to return the number of days between two mentioned date values, DATETIME or TIMESTAMP values.

Syntax of the MySQL DATEDIFF() function

The syntax of the MySQL DATEDIFF() function is given as follows:

DATEDIFF(dt1, dt2)

Parameters or arguments used in MySQL DATEDIFF() function:

There are two parameters accepted by the DATEDIFF() function in MySQL, which are given as follows:

If we pass DATETIME or TIMESTAMP values, the DATEDIFF function only takes the date parts for calculation and ignores the time parts.

dt1: This is the first mentioned date.

dt2: This is the second mentioned date.

Returns:

It will return the number of days between the two mentioned date values.

Application used for DATEDIFF() function:

The DATEDIFF() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

The DATEDIFF function is useful in many cases, e.g., we can calculate an interval in days that the products need to ship to a customer.

Examples of MySQL DATEDIFF() function:

Now we will look into some MySQL DATEDIFF() function examples and will explore how we can use the DATEDIFF function in MySQL.

Example 1 :

In this example, we will get the number of days between two mentioned date values where the date is mentioned in the format of YYYY-MM-DD. Here, date1 is greater than date2, so the return value is positive.

SELECT DATEDIFF("2020-11-20", "2020-11-1");

Output:

19
1 row in set (0.00 sec)

Example 2:

In this example, we will get the number of days between two mentioned date values where the date is mentioned in the format of YYYY-MM-DD. Here the date1 is less than date2, so the return value is negative.

SELECT DATEDIFF("2020-11-12", "2020-11-19");

Output:

-7
1 row in set (0.00 sec)

Example 3:

In this example, we will get the number of days between two mentioned date values where the date is mentioned in the format of YYYY-MM-DD HH-MM-SS.

SELECT DATEDIFF("2020-11-20 09:34:21", "2020-11-17 09:34:21");

Output:

3
1 row in set (0.00 sec)

Example 4:

In this example, we will get the number of days between two mentioned date values where the date is mentioned in the format of YYYY-MM-DD HH-MM-SS. Here time value does not matter as date1 and date2 are taken the same but time is different; still, the output is zero (0).

SELECT DATEDIFF("2020-11-20 09:34:21", "2020-11-20 08:11:23");

Output:

0
1 row in set (0.00 sec)

Some more examples of MySQL DATDIFF function:

Example 1:

SELECT DATEDIFF('2011-08-17','2011-08-17');

Output:

0 day
1 row in set (0.00 sec)

Example 2:

SELECT DATEDIFF('2011-08-17','2011-08-08');

Output:

9 days
1 row in set (0.00 sec)

Example 3:

SELECT DATEDIFF('2011-08-08','2011-08-17');

Output:

9 days
1 row in set (0.00 sec)

Example:

Now let's visualize the following sales table in the sample database.

Sales
Salenumber
Saledate
Shippeddate
Status
Comments
consumernumber

Table- Sales

To calculate the number of days between the required date and the shipped date of the sales, we use the DATEDIFF function as follows:

SELECT
orderNumber,
DATEDIFF(requiredDate, shippedDate) AS daysLeft
FROM
sales
ORDER BY daysLeft DESC;

Output:

SalenumberDaysleft
2016718
2018015
2027713
2017910
2011510
2001710
2017510

The following statement gets all sales whose statuses are in-process and calculates the number of days between the ordered date and the required date:

SELECT
orderNumber,
DATEDIFF(requiredDate, orderDate) remaining_days
FROM
sales
WHERE
status = 'In Process
ORDER BY remaining_days;

For calculating an interval in week or month, we can divide the returned value of the DATEDIFF function by 7 or 30 as the following query:

SELECT
orderNumber,
ROUND(DATEDIFF(requiredDate, orderDate) / 7, 2),
ROUND(DATEDIFF(requiredDate, orderDate) / 30,2)
FROM
sales
WHERE
status = 'In Process';

The important point to be noted is that the ROUND function is used to round the results.

Application of MySQL DATEDIFF() function:

This function is used to return the number of days between the two mentioned date values.

Summary:

In the above context, we have learned how to use the DATEDIFF() function in MySQL to return the number of days between the two mentioned date values.


Related Topics

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL RIGHT() Function

In this context, we will learn how we can use the MySQL RIGHT() Function with proper syntax and good examples. Introduction of MySQL RIGHT() function The RIGHT() function in MySQL is used...

3 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL LENGTH() Function

In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples. Introduction of MySQL LENGTH() function This function in MySQL is used to...

2 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL DAY() function

In this context, we will learn how we can use the MySQL DAY() function with proper syntax and good examples. Introduction of MySQL DAY() function This function in MySQL is used to...

2 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL INSTR() Function

In this context, we will learn how we can use the MySQL INSTR() function with proper syntax and good examples. Introduction of MySQL INSTR() function This function in MySQL returns the location...

4 minutes read.