MySQL DATE_FORMAT '%M' is used for short month?


The %M Date Format is not used for displaying short months like Jan for January, Feb for February, etc. You need to use DATE_FORMAT() function with %b format for short month. The syntax is as follows:

SELECT DATE_FORMAT(yourColumnName, '%d-%b-%y') AS anyVariableName FROM yourTableName;

To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table DateFormatMonthDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> ShippingDate date,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command. The query to insert record is as follows:

mysql> insert into DateFormatMonthDemo(ShippingDate) values('2019-05-21');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DateFormatMonthDemo(ShippingDate) values('2015-05-23');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DateFormatMonthDemo(ShippingDate) values('2019-11-01');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DateFormatMonthDemo(ShippingDate) values('2017-12-24');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DateFormatMonthDemo(ShippingDate) values('2016-01-13');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DateFormatMonthDemo(ShippingDate) values('2015-02-22');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from DateFormatMonthDemo;
+----+--------------+
| Id | ShippingDate |
+----+--------------+
|  1 | 2019-05-21   |
|  2 | 2015-05-23   |
|  3 | 2019-11-01   |
|  4 | 2017-12-24   |
|  5 | 2016-01-13   |
|  6 | 2015-02-22   |
+----+--------------+
6 rows in set (0.00 sec)

Here is the query to display short month names:

mysql> select DATE_FORMAT(ShippingDate, '%d-%b-%y') AS DateDemo from DateFormatMonthDemo;

The following is the output:

+-----------+
| DateDemo  |
+-----------+
| 21-May-19 |
| 23-May-15 |
| 01-Nov-19 |
| 24-Dec-17 |
| 13-Jan-16 |
| 22-Feb-15 |
+-----------+
6 rows in set (0.04 sec)

Updated on: 30-Jul-2019

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements