MySQL query to extract only the day instead of entire date


To extract only the day instead of the entire date, you need to use DAYOFMONTH() function from MySQL.

Let us first create a table −

mysql> create table DemoTable747 (DueDate datetime);
Query OK, 0 rows affected (0.53 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable747 values('2019-01-31') ;
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable747 values('2018-12-01');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable747 values('2017-09-14');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable747 values('2016-07-21');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable747;

This will produce the following output -

+---------------------+
| DueDate             |
+---------------------+
| 2019-01-31 00:00:00 |
| 2018-12-01 00:00:00 |
| 2017-09-14 00:00:00 |
| 2016-07-21 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)

Following is the query to extract only the day instead of entire date −

mysql> select dayofmonth(DueDate) from DemoTable747;

This will produce the following output -

+---------------------+
| dayofmonth(DueDate) |
+---------------------+
|                  31 |
|                   1 |
|                  14 |
|                  21 |
+---------------------+
4 rows in set (0.00 sec)

Updated on: 03-Sep-2019

506 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements