Convert datetime to get month name in MySQL?


To get only month name, the syntax is as follows −

select date_format(yourColumnName,'%M %Y') from yourTableName;

Let us first create a table −

mysql> create table DemoTable1619
    -> (
    -> ArrivalTime datetime
    -> );
Query OK, 0 rows affected (0.45 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1619 values(now());
Query OK, 1 row affected (0.40 sec)
mysql> insert into DemoTable1619 values(curdate());
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1619 values('2019-12-31');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1619;

This will produce the following output −

+---------------------+
| ArrivalTime         |
+---------------------+
| 2019-10-20 15:02:12 |
| 2019-10-20 00:00:00 |
| 2019-12-31 00:00:00 |
+---------------------+
3 rows in set (0.00 sec)

Following is the query to convert datetime to get month name in MySQL −

mysql> select date_format(ArrivalTime,'%M %Y') from DemoTable1619;

This will produce the following output −

+----------------------------------+
| date_format(ArrivalTime,'%M %Y') |
+----------------------------------+
| October 2019                     |
| October 2019                     |
| December 2019                    |
+----------------------------------+
3 rows in set (0.00 sec)

Updated on: 08-Nov-2019

259 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements