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

536
Vistas
Difference between two dates in MySQL with DATE() function

I am using DATE() function to calculate the difference between two dates in MySQL

value of SYSDATE() function is following

select SYSDATE();
2020-07-15 12:16:07.0

When I am using date from same month, it is giving correct result

select DATE(SYSDATE())- DATE('2020-07-13');
2

But when I am using date from last month it is giving difference as 86 instead of 16;

select DATE(SYSDATE())- DATE('2020-06-29');
86

Edit: I am aware that we can use DATEDIFF() but I want to verify why DATE() function is giving results like this since we are already using this in code

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

0

MySQL doesn't support subtracting one date from another. The code

SELECT DATE '2020-07-15' - DATE '2020-06-29';

should hence result in an error, but MySQL silently converts this to this instead:

SELECT 20200715 - 20200629;

Seeing that you want to subtract two values, it assumes that you want to work with numbers. Dates are not numbers, but their internal representation yyyymmdd can be represented numerically. So, while CAST(DATE '2020-07-15 ' AS int) fails with a syntax error, as it should, MySQL is not consistent, when it comes to subtraction. It generates the numbers 20200715 and 20200629 and works with these.

I consider this a bug. MySQL should either raise an exception or return an INTERVAL when subtracting one DATE from another.

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