Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda