2

Introduction

I have a table of buses in MySQL and each bus has a few arrival timings. I need to sort the buses by the first arrival.

Unsorted Table

+-------+-------------------------------+
|  bus  |             time              |
+-------+-------------------------------+
| Bus A | 8 a.m.<br/>9 a.m.             |
| Bus B | 12 p.m.<br/>1 p.m.<br/>2 p.m. |
| Bus C | 10 a.m.                       |
| Bus D | 9:30 a.m.                     |
+-------+-------------------------------+

Sorted (Wrong)

+-------+-------------------------------+
|  bus  |             time              |
+-------+-------------------------------+
| Bus C | 10 a.m.                       |
| Bus B | 12 p.m.<br/>1 p.m.<br/>2 p.m. |
| Bus A | 8 a.m.<br/>9 a.m.             |
| Bus D | 9:30 a.m.                     |
+-------+-------------------------------+

Sorted (Correct)

+-------+-------------------------------+
|  bus  |             time              |
+-------+-------------------------------+
| Bus A | 8 a.m.<br/>9 a.m.             |
| Bus D | 9:30 a.m.                     |
| Bus C | 10 a.m.                       |
| Bus B | 12 p.m.<br/>1 p.m.<br/>2 p.m. |
+-------+-------------------------------+

Query

I've tried using this answer, but it doesn't seem to work. It might be due to formatting issues -- "2:00 AM" vs "2:00 a.m.".

SELECT bus_timing
FROM buses
ORDER BY STR_TO_DATE(bus_timing, '%l:%i %p')

Question

So, how can I sort these data according to the timings without changing the data at all?

19
  • which data type is time column ? Commented Nov 7, 2016 at 10:50
  • @scaisEdge varchar (50) Commented Nov 7, 2016 at 10:51
  • 1
    why are there nulls in your bus column? This is not excel this is a database Commented Nov 7, 2016 at 10:51
  • 1
    your data is still wrong. The times shouldn't be in a single column separated by line breaks. In fact they shouldn't be in a single column at all Commented Nov 7, 2016 at 10:55
  • 1
    No, no, no. Start over, with a properly designed database. Commented Nov 7, 2016 at 11:08

3 Answers 3

1

The STR_TO_DATE format and approach looks good. So the data might be the problem.

This

ORDER BY STR_TO_DATE(REPLACE(UCASE(bus_timing),'.',''), '%l:%i %p')

will remove the dots in a.m. and p.m. and change them to upper case, which will be more in line with the expected %p format

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but it doesn't seem to work. Would it be possible to remove everything after the <br/> part?
Wait... you have those <br/> in the database?? I thought it was just a formatting issue while copy/pasting here
I also see that you edited your question and changed how the data is displayed. For the bus B, when I asnwred you had one row for each time, and now everything is grouped together. Very confusing
1

If you can format the date this way 8 00 am instead of 8 a.m. you could use

select STR_TO_DATE( substr('8 30 am<br/>9 a.m. ', 1, 
              LOCATE('<br/>','8 30 am<br/>9 a.m. ') -1), '%l%i %p');

You should obtain the time only for the first value and sort for this

Comments

1

Use substring_index() to break the string apart and get the first element. Then sort by the first element.

So:

order by str_to_date(substring_index(time, '<', 1), '%l:%i %p')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.