0

Hello I have the following column and I want to order it by date from earliest to latest. the format looks like this.

08/16/2019 08:09:51 AM

I came up with this but its giving me an error.

ORDER BY date_format(STR_TO_DATE(`END_TIME`,'%m-%d-%y %h:%i:%s %p'),'%Y-%m-%d %H:%i:%s')

I couldn't strictly order it by date because 2 PM came before 8 AM which is wrong so I tried to put it in the correct format, I feel like im close. Any ideas?

stack flow
  • 75
  • 6

1 Answers1

1

Maybe I am wrong but i do not see nothing wrong here:

select to_char(to_date(Date_c,'mm/dd/yyyy hh:mi:ss AM'),'mm/dd/yyyy hh:mi:ss AM') 
from myTable
order by to_date(Date_c,'mm/dd/yyyy hh:mi:ss AM');

That is whit this:

select *
from myTable
order by to_date(Date_c,'mm/dd/yyyy hh:mi:ss AM');

Here is the DEMO

VBoka
  • 8,995
  • 3
  • 16
  • 24
  • 1
    This is the correct answer. Thank you so much :) my format was wrong i had AM before but I thought it wouldnt pick up the PM. – stack flow Nov 19 '19 at 17:57