
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we calculate the Date in MySQL using functions?
In MySQL, we can use the following functions to calculate the Date −
- CURDATE() Function − Basically it returns the current date of the computer.
- YEAR() Function − It returns the year of the specified date.
- MONTH() function − It returns the month of the specified date.
- DAY() Function − It returns the day of the specified date.
- RIGHT() Function − It returns the number of character as specified within the function from the given date. The part of the expression that compares the returns from RIGHT() function evaluates 1 or 0.
To understand it, consider the data, as follows, from a table named ‘Collegedetail’ −
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 following query, we calculated the DATE by using all different date functions −
mysql> Select ID, Estb, CURDATE(), YEAR(Estb), MONTH(Estb), DAY(Estb), (RIGHT(CURDATE(),5) < RIGHT(estb,5))As 'Return' 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 'YEARS_OLD' 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)
- Related Articles
- How can we combine functions in MySQL?
- How can we offload the time/date handling in MySQL?
- How can we stuff a string with another one using MySQL functions?
- How can we obtain the part of a date in MySQL?
- How can we distinguish between MySQL IFNULL() and NULLIF() functions?
- How can we calculate the difference between two time values in MySQL?
- How can we create MySQL stored procedure to calculate the factorial?
- How can we extract the Year and Month from a date in MySQL?
- How can we format a date using the Jackson library in Java?
- What are MySQL stored functions and how can we create them?
- In MySQL, how can we display the date in other format specified by the user?
- How can we map multiple date formats using Jackson in Java?
- How can we see only the list of stored functions in a particular MySQL database?
- How can we find the most recent and most oldest date from a table with the help of MySQL MAX() and MIN() functions?
- How can we insert current date automatically in a column of MySQL table?

Advertisements