user3775081
user3775081

Reputation:

Apply sorting to mysql table value stored like 9:15 AM,9:30 AM

Given the following format:

9:00 AM
9:15 AM
...

How can I sort a column in mySQL in descending or ascending order?

Upvotes: 0

Views: 35

Answers (2)

user1922137
user1922137

Reputation:

You can use STR_TO_DATE function:

For example:

SELECT * FROM table_name ORDER BY STR_TO_DATE(time_coulmn,'%h:%i %p') ASC;

Upvotes: 2

chiastic-security
chiastic-security

Reputation: 20520

Unless something silly has been done with the database, the data won't really be stored as a string in that form. Databases are good at storing date fields, and then sorting them when they return results; it's up to the application to work out how to display them.

If you're really storing the times as strings, the right thing to do is to stop, and change the way you're storing them. Store them as timestamps (DateTime in MySQL) , and then format them appropriately when you retrieve them.

Upvotes: 1

Related Questions