
- 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 sort time in AM/ PM in MySQL?
To sort time in AM/PM in MySQL, you can use ORDER BY STR_TO_DATE().
Following is the syntax −
select yourColumnName from yourTableName ORDER BY STR_TO_DATE(yourColumnName , '%l:%i %p');
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserLogoutTime varchar(200) ); Query OK, 0 rows affected (0.97 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable(UserLogoutTime) values('09:45 PM'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserLogoutTime) values('11:56 AM'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserLogoutTime) values('01:01 AM'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(UserLogoutTime) values('02:01 PM'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(UserLogoutTime) values('04:10 PM'); Query OK, 1 row affected (0.15 sec)
Display records from the table using select command −
mysql> select *from DemoTable;
This will produce the following output −
+----+----------------+ | Id | UserLogoutTime | +----+----------------+ | 1 | 09:45 PM | | 2 | 11:56 AM | | 3 | 01:01 AM | | 4 | 02:01 PM | | 5 | 04:10 PM | +----+----------------+ 5 rows in set (0.00 sec)
Here is the query to sort time In AM/ PM in MySQL −
mysql> select UserLogoutTime from DemoTable ORDER BY STR_TO_DATE(UserLogoutTime, '%l:%i %p');
This will produce the following output −
+----------------+ | UserLogoutTime | +----------------+ | 01:01 AM | | 11:56 AM | | 02:01 PM | | 04:10 PM | | 09:45 PM | +----------------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL format time with lowercase am/pm?
- Java Program to Format time in AM-PM format
- Format MySQL CURRENT_TIMESTAMP to AM & PM?
- Display AM/PM time marker with SimpleDateFormat(“a”) in Java
- How do you display JavaScript datetime in 12hour AM/PM format?
- Java Program to display hour and minute in AM or PM
- Java Program to display hour in K (0-11 in AM/PM) format
- Display hour in h (1-12 in AM/PM) format in Java
- Display hour in hh (01-12 in AM/PM) format in Java
- Convert PHP variable “11:00 AM” to MySQL time format?
- How to sort more than one column at a time in MySQL?
- Sort by date & time in descending order in MySQL?
- How to sort domain names in MySQL?
- How to convert varchar “time” to real time in MySQL?
- How to sort an alphanumeric column in MySQL?

Advertisements