How can we fetch month and day from a given date in MySQL?

MySQLMySQLi Database

It can be done inflowing two ways −

(A) With the help of EXRACT() function - EXTRACT() function can fetch any part from MySQL TIMESTAMP value. Following is the example of fetching month and day from a given date.

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

mysql> Select EXTRACT(day from '2017-10-22')AS 'DAY';
+------+
| DAY  |
+------+
|   22 |
+------+
1 row in set (0.00 sec)

(B) With the help of MONTH() or DAY() function -  Rather than passing month and day as one of the arguments of EXTRACT() function, we can use MONTH() and DAY() function to fetch the value of MONTH and DAY from a given date as follows −

mysql> Select DAY('2017-10-22')AS 'DAY';
+------+
| DAY  |
+------+
|   22 |
+------+
1 row in set (0.00 sec)

mysql> Select MONTH('2017-10-22')AS 'MONTH';
+-------+
| MONTH |
+-------+
|    10 |
+-------+
1 row in set (0.00 sec)
raja
Published on 19-Feb-2018 16:08:42
Advertisements