0

I'm storing datetime in varchar in my table. This my datetime format : 12/13/2019 12:05:30 PM. I want to sort these date in descending order. This is my sql

SELECT CreatedOn FROM advweb_usertemplatedesign Where UserId = 'fa69147ca33746cd8700a892298b11de' And ActiveStatus = 1 Order by CreatedOn Desc Limit 20 

The result I'm getting is

12/5/2019 5:48:19 PM 12/4/2019 4:49:10 PM 12/3/2019 5:57:16 PM 12/13/2019 12:05:30 PM 12/12/2019 6:01:31 PM 

Instead of

12/13/2019 12:05:30 PM 12/12/2019 6:01:31 PM 12/5/2019 5:48:19 PM 12/4/2019 4:49:10 PM 12/3/2019 5:57:16 PM 

How to properly sort it? Thank you.

3
  • 1
    you should store date in datetime type Commented Dec 13, 2019 at 4:30
  • Does this answer your question? MySQL date format DD/MM/YYYY select query? Commented Dec 13, 2019 at 4:34
  • @DevsiOdedra no, it doesn't. Now getting 11/26/2019 3:21:36 PM, 11/27/2019 4:56:11 PM, 12/13/2019 12:05:30 PM, 11/27/2019 5:35:32 PM....it kinda mixed up now. Commented Dec 13, 2019 at 4:39

1 Answer 1

1

You should avoid storing your dates as strings. As a temporary workaround, you may use MySQL's STR_TO_DATE function here:

SELECT CreatedOn FROM advweb_usertemplatedesign WHERE UserId = 'fa69147ca33746cd8700a892298b11de' AD ActiveStatus = 1 ORDER BY STR_TO_DATE(CreatedOn, '%m/%d/%Y %r') DESC LIMIT 20; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.