How to apply EXTRACT() function on the dates stored in MySQL table?


We can apply EXTRACT() function on the dates stored in MySQL table in the following way −

The following query is showing what dates are entered in table ‘testing’

mysql> Select * from testing;
+-------------+---------------------+
| StudentName | Dateofreg           |
+-------------+---------------------+
| Ram         | 2017-10-28 21:24:24 |
| Shyam       | 2017-10-28 21:24:30 |
| Mohan       | 2017-10-28 21:24:47 |
| Gaurav      | 2017-10-29 08:48:33 |
+-------------+---------------------+
4 rows in set (0.00 sec)

Now, we can apply EXTRACT() function, to obtain the value of the year, on ‘testing’ table as follows −

mysql> Select EXTRACT(Year from dateofreg)AS YEAR from testing;
+------+
| YEAR |
+------+
| 2017 |
| 2017 |
| 2017 |
| 2017 |
+------+
4 rows in set (0.00 sec)

Similarly, we can apply EXTRACT() function, to obtain the value of day, on ‘testing’ table as follows −

mysql> Select EXTRACT(day from dateofreg)AS DAY from testing;
+-----+
| DAY |
+-----+
| 28  |
| 28  |
| 28  |
| 29  |
+-----+
4 rows in set (0.00 sec)

Updated on: 29-Jan-2020

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements