- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change format of dates in a table column with a MySQL query?
To change format of dates, use the DATE_FORMAT() function. Let us first create a table −
mysql> create table DemoTable1906 ( DueTime datetime ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1906 values(now()); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1906 values(curdate()); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1906 values('2017-10-11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1906 values('2015-01-12'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1906 values('2018-04-25'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1906;
This will produce the following output −
+---------------------+ | DueTime | +---------------------+ | 2019-12-11 13:54:22 | | 2019-12-11 00:00:00 | | 2017-10-11 00:00:00 | | 2015-01-12 00:00:00 | | 2018-04-25 00:00:00 | +---------------------+ 5 rows in set (0.00 sec)
Here is the query to change the format of dates −
mysql> select date_format(DueTime,'%d-%m-%Y') from DemoTable1906;
This will produce the following output −
+---------------------------------+ | date_format(DueTime,'%d-%m-%Y') | +---------------------------------+ | 11-12-2019 | | 11-12-2019 | | 11-10-2017 | | 12-01-2015 | | 25-04-2018 | +---------------------------------+ 5 rows in set (0.00 sec)
Advertisements