| re: Sort by time (hh:mm:am/pm - format) in text string
00steve wrote:
[color=blue]
> Hi,
>
> I have a group of records that I need to sort by time field. The time
> field is a text string (should've been a date/time - I know -doh!) I
> was wondering if anyone knew how to write a query to sort in Ascending
> order with the earliest appearing first.
>
> The times are stored as follows:
>
> 10:30pm
> 11:30pm[/color]
Try something built from this MySQL query :
select time_format(time, '%I:%i%p') as the_time from timetest order by
the_time asc;
----------------------- Sample output ----------------------------
mysql> select * from timetest;
+---------+
| time |
+---------+
| 10:15pm |
| 12:14pm |
| 12:15am |
| 9:30am |
+---------+
4 rows in set (0.00 sec)
mysql> select time_format(time, '%I:%i%p') as the_time from timetest
order by the_time asc;
+----------+
| the_time |
+----------+
| 09:30AM |
| 10:15AM |
| 12:14PM |
| 12:15PM |
+----------+
4 rows in set (0.00 sec)
----------------------------- ends --------------------------------
Hope this helps,
Regards,
Andy |