How can we extract the Year and Month from a date in MySQL?


It can be done with the following three ways in MySQL

By using EXTRACT() function

 For extracting YEAR and MONTH collectively then we can use the EXTRACT function. We need to provide the YEAR_MONTH as an argument for this function. To understand it, consider the following function using the data from table ‘Collegedetail’ −

mysql> Select EXTRACT(YEAR_MONTH From estb) from collegedetail;
+-------------------------------+
| EXTRACT(YEAR_MONTH From estb) |
+-------------------------------+
|                        201005 |
|                        199510 |
|                        199409 |
|                        200107 |
|                        201007 |
+-------------------------------+
5 rows in set (0.00 sec)

By using DATE_FORMAT() function

 It can extract the year and the month collectively as well as separately. As the name suggests we can also format its output. To understand it, consider the following example which uses the data from table ‘Collegedetail’ −

mysql> Select DATE_FORMAT(estb, '%Y %m') from collegedetail;
+----------------------------+
| DATE_FORMAT(estb, '%Y %m') |
+----------------------------+
| 2010 05                    |
| 1995 10                    |
| 1994 09                    |
| 2001 07                    |
| 2010 07                    |
+----------------------------+
5 rows in set (0.00 sec)

mysql> Select DATE_FORMAT(estb, '%Y') from Collegedetail;
+-------------------------+
| DATE_FORMAT(estb, '%Y') |
+-------------------------+
| 2010                    |
| 1995                    |
| 1994                    |
| 2001                    |
| 2010                    |
+-------------------------+
5 rows in set (0.00 sec)

mysql> Select DATE_FORMAT(estb, '%m') from Collegedetail;
+-------------------------+
| DATE_FORMAT(estb, '%m') |
+-------------------------+
| 05                      |
| 10                      |
| 09                      |
| 07                      |
| 07                      |
+-------------------------+
5 rows in set (0.00 sec)

mysql> Select DATE_FORMAT(estb, '%M') from Collegedetail;
+-------------------------+
| DATE_FORMAT(estb, '%M') |
+-------------------------+
| May                     |
| October                 |
| September               |
| July                    |
| July                    |
+-------------------------+
5 rows in set (0.10 sec)

By using two different functions, YEAR() and MONTH()

 it will extract the Year and month separately by using two different functions. To understand it, consider the following example which uses the data from table ‘Collegedetail’ −

mysql> Select YEAR(estb) AS 'Year', MONTH(estb) As 'MONTH' From collegedetail;
+------+-------+
| Year | MONTH |
+------+-------+
| 2010 |     5 |
| 1995 |    10 |
| 1994 |     9 |
| 2001 |     7 |
| 2010 |     7 |
+------+-------+
5 rows in set (0.00 sec)

Monica Mona
Monica Mona

Student of life, and a lifelong learner

Updated on: 22-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements