Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda