;

How to Calculate Total Number of days between two specific dates using DATEDIFF function in MySQL


Tutorialsrack 26/02/2020 SQL MySQL

In this article, you will learn how to calculate total no. of days between two specific dates using the DATEDIFF function in MySQL. 

To get a total no. of days between two specific dates, we use the DATEDIFF function of MySQL.

Syntax  of DATEDIFF Function

Syntax
DATEDIFF( date1, date2 );

This function takes two parameters:

  • Parameter 1: date1 A date/DateTime value
  • Parameter 1: date2 A date/DateTime value

Here are some example to calculate the total no. of days between two specific dates using the DATEDIFF function:

Example 1: In this Example, We will compare Datetime values with date and time part to calculate the total number of days between two specific dates.

Example 1
-- When used with datetime values, only the date part is used to compare the date.
select datediff('2016-04-14 11:59:00', '2016-04-13 12:00:00') AS 'Total No. of Days Between Two Specific Date';
Output

Total No. of Days Between Two Specific Date

------------------------------------------------------------------

1

Example 2: In this Example, We will compare only the date part to calculate the total number of days between two specific dates.

Example 2
-- When used with date Part, only the date part is used to compare the date.
SELECT DATEDIFF('2020-02-10', '2019-12-25') AS 'Total No. of Days Between Two Specific Date';
Output

Total No. of Days Between Two Specific Date

-----------------------------------------------------------------

47

Example 3: In this Example, We will compare parameter 1 with DateTime values and parameter 2 only the date part to calculate the total number of days between two specific dates.

Example 3
-- When We Compare Datetime values and Only with Date Part
SELECT DATEDIFF('2020-02-10 11:41:14', '2019-12-25') AS 'Total No. of Days Between Two Specific Date';
Output

Total No. of Days Between Two Specific Date

-----------------------------------------------------------------

47

Example 4: In this example, if we pass the smaller date in the first parameter than the second parameter, then this function will return the negative output.

Example 4
--  When First Parameter is smaller than the second Parameter, it returns a negative value
SELECT DATEDIFF('2019-12-31', '2020-02-10') AS 'Total No. of Days Between Two Specific Date';
Output

Total No. of Days Between Two Specific Date

----------------------------------------------------------------

-41

I hope this article will help you to understand how to calculate the total no. of days between two specific dates using the DATEDIFF function in MySQL. 

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags