Select*fromCollegedetail;+------+----"/> Select*fromCollegedetail;+------+----">

Home  >  Article  >  Database  >  How can we use functions to calculate dates in MySQL?

How can we use functions to calculate dates in MySQL?

PHPz
PHPzforward
2023-08-30 21:41:021486browse

How can we use functions to calculate dates in MySQL?

In MySQL, we can use the following functions to calculate dates-

  • CURDATE() function- Basically It returns the computer's current date.
  • YEAR() Function - It returns the year for the specified date.
  • MONTH () Function - Returns the month of the specified date.
  • DAY() function - Returns the day of the specified date.
  • RIGHT() function - It returns the number of characters specified in the function since the given date. The part of the expression that compares the return value of the RIGHT() function evaluates to 1 or 0.

To understand it, consider the data from the table named "Collegedetail" as shown below -

mysql> Select * from Collegedetail;
+------+---------+------------+
| ID   | Country | Estb       |
+------+---------+------------+
| 111  | INDIA   | 2010-05-01 |
| 130  | INDIA   | 1995-10-25 |
| 139  | USA     | 1994-09-25 |
| 1539 | UK      | 2001-07-23 |
| 1545 | Russia  | 2010-07-30 |
+------+---------+------------+
5 rows in set (0.00 sec)

In the query below, we are using all the different date functions Calculated DATE -

mysql> Select ID, Estb, CURDATE(), YEAR(Estb), MONTH(Estb), DAY(Estb), (RIGHT(CURDATE(),5) < RIGHT(estb,5))As &#39;Return&#39; FROM Collegedetail;
+------+------------+------------+------------+-------------+-----------+--------+
| ID   | Estb       | CURDATE()  | YEAR(Estb) | MONTH(Estb) | DAY(Estb) | Return |
+------+------------+------------+------------+-------------+-----------+--------+
| 111  | 2010-05-01 | 2017-11-30 | 2010       | 5           | 1         | 0      |
| 130  | 1995-10-25 | 2017-11-30 | 1995       | 10          | 25        | 0      |
| 139  | 1994-09-25 | 2017-11-30 | 1994       | 9           | 25        | 0      |
| 1539 | 2001-07-23 | 2017-11-30 | 2001       | 7           | 23        | 0      |
| 1545 | 2010-07-30 | 2017-11-30 | 2010       | 7           | 30        | 0      |
+------+------------+------------+------------+-------------+-----------+--------+
5 rows in set (0.00 sec)

mysql> Select ID, estb, CURDATE(),((YEAR(CURDATE())-YEAR(estb))-(RIGHT(CURDATE(),5)<RIGHT(estb,5))) AS &#39;YEARS_OLD&#39; from collegedetail;
+------+------------+------------+-----------+
| ID   | estb       | CURDATE()  | YEARS_OLD |
+------+------------+------------+-----------+
| 111  | 2010-05-01 | 2017-11-30 | 7         |
| 130  | 1995-10-25 | 2017-11-30 | 22        |
| 139  | 1994-09-25 | 2017-11-30 | 23        |
| 1539 | 2001-07-23 | 2017-11-30 | 16        |
| 1545 | 2010-07-30 | 2017-11-30 | 7         |
+------+------------+------------+-----------+
5 rows in set (0.00 sec)

The above is the detailed content of How can we use functions to calculate dates in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete