How can we obtain the part of a date in MySQL?


By using EXTRACT() function we can obtain the part from current date or from given date. The parts of the date can be obtained in the form of year, month, days, hours, minutes, seconds and microseconds.

Examples

mysql> Select EXTRACT(Year from NOW()) AS YEAR;
+-------+
| YEAR  |
+-------+
|   2017|
+-------+
1 row in set (0.00 sec)

Above MySQL query will obtain the year from current date.

mysql> Select EXTRACT(Month from '2017-09-21')AS MONTH;
+-------+
| MONTH |
+-------+
|     9 |
+-------+
1 row in set (0.00 sec)

Above MySQL query will obtain the month from given date.

Updated on: 20-Jun-2020

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements