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

128
Vistas
MySQL date search doesn't work expectedly on datetime field if time is also included in the query

I am wondering why the following query doesn't yield any result if time is also included in the query?

SELECT * FROM `tb_posts` WHERE DATE(`post_date_published`) = '2013-05-05 16:20:58'

However, when time is excluded from the search parameter

SELECT * FROM `tb_posts` WHERE DATE(`post_date_published`) = '2013-05-05'

Then it fetches the related record from database.

enter image description here

How can we fetch record still using time in the query?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

When you compare a DATE with an expression that isn't also DATE, it treats the date as being at time 00:00:00. This is mentioned in the documentation:

When you compare a DATE, TIME, DATETIME, or TIMESTAMP to a constant string with the <, <=, =, >=, >, or BETWEEN operators, MySQL normally converts the string to an internal long integer for faster comparison (and also for a bit more “relaxed” string checking).

So it's comparing the numeric representation of the date with the numeric representation of the datetime string constant, and they don't match because of the different times.

So you need to remove the time from the string:

SELECT * FROM `tb_posts` WHERE DATE(`post_date_published`) = DATE('2013-05-05 16:20:58')
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