
- 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
MySQL date format DD/MM/YYYY select query?
Format the date DD/MM/YYYY using select and order by in descending order. The syntax is as follows −
SELECT *FROM yourTableName where yourDatetimeColumnName order by STR_TO_DATE(yourDatetimeColumnName,’%d/%m%Y’) desc;
The above syntax will give the date in descending order. To understand the above syntax, let us first create a table. The query to create a table is as follows −
mysql> create table DateFormatWithSelect -> ( -> UserId int, -> UserName varchar(100), -> UserLoginDatetime varchar(100) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into DateFormatWithSelect values(101,'John','20/10/2016'); Query OK, 1 row affected (0.11 sec) mysql> insert into DateFormatWithSelect values(102,'David','21/09/2015'); Query OK, 1 row affected (0.20 sec) mysql> insert into DateFormatWithSelect values(103,'Carol','21/12/2018'); Query OK, 1 row affected (0.10 sec) mysql> insert into DateFormatWithSelect values(104,'Mike','2/8/2014'); Query OK, 1 row affected (0.16 sec) mysql> insert into DateFormatWithSelect values(105,'Sam','21/11/2017'); Query OK, 1 row affected (0.12 sec) mysql> insert into DateFormatWithSelect values(106,'Bob','21/12/2013'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select command. The query is as follows −
mysql> select *from DateFormatWithSelect;
The following is the outpu −
+--------+----------+-------------------+ | UserId | UserName | UserLoginDatetime | +--------+----------+-------------------+ | 101 | John | 20/10/2016 | | 102 | David | 21/09/2015 | | 103 | Carol | 21/12/2018 | | 104 | Mike | 2/8/2014 | | 105 | Sam | 21/11/2017 | | 106 | Bob | 21/12/2013 | +--------+----------+-------------------+ 6 rows in set (0.00 sec)
Here is the SELECT that formats the date in DD/MM/YYYY format −
mysql> select *from DateFormatWithSelect -> where UserLoginDatetime order by str_to_date(UserLoginDatetime,'%d/%m/%Y') desc;
Output
+--------+----------+-------------------+ | UserId | UserName | UserLoginDatetime | +--------+----------+-------------------+ | 103 | Carol | 21/12/2018 | | 105 | Sam | 21/11/2017 | | 101 | John | 20/10/2016 | | 102 | David | 21/09/2015 | | 104 | Mike | 2/8/2014 | | 106 | Bob | 21/12/2013 | +--------+----------+-------------------+ 6 rows in set, 6 warnings (0.00 sec)
- Related Articles
- Get date format DD/MM/YYYY with MySQL Select Query?
- MySQL query to convert YYYY-MM-DD to DD Month, YYYY date format
- Convert MySQL date format from yyyy-mm-ddThh:mm:ss.sssZ to yyyy-mm-dd hh:mm:ss ?
- MySQL date format to convert dd.mm.yy to YYYY-MM-DD?
- How to format JavaScript date into yyyy-mm-dd format?
- Get today's date in (YYYY-MM-DD) format in MySQL?
- Java Program to format date in mm-dd-yyyy hh:mm:ss format
- How to parse Date from String in the format: dd/MM/yyyy to dd/MM/yyyy in java?
- Accepting date strings (MM-dd-yyyy format) using Java regex?
- Program to reformat date in YYYY-MM-DD format using Python
- Change date format (in DB or output) to dd/mm/yyyy in PHP MySQL?
- How to insert mm/dd/yyyy format dates in MySQL?
- How to format a string to date in as dd-MM-yyyy using java?
- MySQL - Convert YYYY-MM-DD to UNIX timestamp
- How to convert MM/YY to YYYY-MM-DD in MYSQL?

Advertisements