Change the curdate() (current date) format in MySQL

MySQLMySQLi Database

The current date format is ‘YYYY-mm-dd’. To change current date format, you can use date_format().

Let us first display the current date −

mysql> select curdate();

This will produce the following output −

+------------+
| curdate()  |
+------------+
| 2019-08-08 |
+------------+
1 row in set (0.00 sec)

Following is the query to change curdate() (current date) format −

mysql> select date_format(curdate(), '%m/%d/%Y');

This will produce the following output −

+------------------------------------+
| date_format(curdate(), '%m/%d/%Y') |
+------------------------------------+
| 08/08/2019                         |
+------------------------------------+
1 row in set (0.00 sec)

Let us first create a table −

mysql> create table DemoTable (
   ArrivalDate date
);
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-01-10');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('2016-12-18');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| ArrivalDate |
+-------------+
| 2019-01-10  |
| 2016-12-18  |
+-------------+
2 rows in set (0.00 sec)

Following is the query to change date format −

mysql> select date_format(ArrivalDate, '%m/%d/%Y') from DemoTable;

This will produce the following output −

+--------------------------------------+
| date_format(ArrivalDate, '%m/%d/%Y') |
+--------------------------------------+
| 01/10/2019                           |
| 12/18/2016                           |
+--------------------------------------+
2 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 17:53:43
Advertisements