×

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 Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL PERIOD_ADD() function

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

2 minutes read.

MySQL NOW() function

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

2 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL PERIOD_DIFF() function

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

2 minutes read.

MySQL MONTHNAME() function

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

3 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 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 INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL LOG() function

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

3 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL ROUND() function

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

3 minutes read.

MySQL RAND() function

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

3 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 minutes read.

MySQL RADIANS() function

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

2 minutes read.