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

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

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 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