Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

529
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!