Select date from MySQL and format to text?

MySQLMySQLi Database

To select date and format, use SELECT DATE_FORMAT(). Following is the syntax −

Syntax

select date_format(yourColumnName, '%e %b %y') from yourTableName;

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> DueDate date
   -> );
Query OK, 0 rows affected (0.52 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-01-11');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('2019-12-21');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('2019-09-15');
Query OK, 1 row affected (0.07 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 2019-01-11 |
| 2019-12-21 |
| 2019-09-15 |
+------------+
3 rows in set (0.00 sec)

Following is the query to select a date from MySQL and format to text −

mysql> select date_format(DueDate, '%e %b %y') from DemoTable;

This will produce the following output −

+----------------------------------+
| date_format(DueDate, '%e %b %y') |
+----------------------------------+
| 11 Jan 19                        |
| 21 Dec 19                        |
| 15 Sep 19                        |
+----------------------------------+
3 rows in set (0.00 sec)
raja
Published on 11-Dec-2019 11:42:14
Advertisements