Display month names and year from a column with date records with MySQL


Let us first create a −

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

Insert some records in the table using insert −

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 −

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: 11-Nov-2019

339 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements