
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- MySQL select distinct dates from datetime column in a table?
- Create a temporary table with dates in MySQL
- MySQL query to find latest 3 dates in a table and the resultant dates shouldn’t be duplicate
- MySQL query to count number of duplicate values in a table column
- Change the column name from a MySQL table with Student record?
- Update a MySQL column with JSON format?
- Select the table name as a column in a UNION select query with MySQL?
- How to change date format with DATE_FORMAT() in MySQL?
- How to change the column position of MySQL table without losing column data?
- Add a new column and index to an existing table with ALTER in a single MySQL query?
- How to query between two dates in MySQL?
- How to change the date in a table with date records with MySQL?
- Selecting a value in custom order from another column in a MySQL table with a single query
- MySQL query to select all the records only from a specific column of a table with multiple columns
- Display distinct dates in MySQL from a column with date records

Advertisements