Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
MYSQL ORDER BY date of type Varchar

I want to sort my resultset with respect to date which is of varchar type and in this format (dd/MMM/YYYY) but the problem is its not working fine. I am using this query after a lot of search but still unable to get my desired result.

SELECT `batch_expiry` FROM `batchwise_stock` ORDER BY str_to_date(`batch_expiry`, '%d/%m/%Y')

Sorting is Not doing properly but getting this sorted dates

29/Apr/2020
03/May/2020
16/May/2020
12/May/2020
14/May/2020

Please need help, Thanks in advance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The issue is that your dates are not in the format %Y/%m/%d. In that format, the month is numeric. Your format is %Y/%b/%d, so use that:

order by str_to_date(batch_expiry, '%d/%b/%Y')

Here is a db<>fiddle.

The result of your expression is NULL, because the date column cannot be converted into that format. All the order by keys are then equivalent, so the ordering in the result set is arbitrary.

As Strawberry points out in a comment, you should store your date values using the proper type, not as a string.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!