1

how to sort the table for time . that is from 6.00 AM t0 10.00 PM

 name    source destination time 
 gokul   xxx    yyyy        10.00 PM
 abc     xxx    yyyy        6.00 AM

I tried this query in side subquery still not working

Select * from (
  Select * from trips where date = '27-09-2013' 
  and time like '%PM' or '%pm' ORDER BY time
) AND (
  Select * from trips where date = '27-09-2013' 
  and time like '%AM' or '%am' ORDER BY time
) AS TIME 
ORDER BY TIME(time) DESC' 
2
  • 1
    is time in string form, with AM and PM?, or in actual timestamp form? Commented Sep 27, 2013 at 10:54
  • its a varchar @AdrianBR and i tried ORDER BY and LIKE but its not wokring Commented Sep 27, 2013 at 11:09

1 Answer 1

3

use this query:

SELECT * FROM tabel_name 
    ORDER BY STR_TO_DATE(`time`,'%h.%i%p');

SQL FIDDLE working example.

EDITED:

After seeing your query i am posting the modified query for you.

SELECT * FROM trips 
    WHERE `date` = '27-09-2013' 
       AND ((`time` like '%PM' OR '%pm') 
          OR (`time` like '%AM' OR '%am')) ORDER BY STR_TO_DATE(`time`,'%h.%i%p') DESC
Sign up to request clarification or add additional context in comments.

1 Comment

@gokul can you post your table structure also.

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.