Forum

Posted by Will, Jan. 29, 2023, 7:19 a.m.

Q153 - DATEDIFF vs. Subtracting Two Dates

Hi Leon - I believe the solution here may be incorrect due to the issue of how MySQL calculates dates as you previously mentioned in this thread.

This query:

SELECT project_id,
               DATEDIFF(end_date, start_date) AS datediff_calc,
               (end_date - start_date) AS simple_subtraction
  FROM project_detail;

produces the following table:

project_id datediff_calc simple_subtraction
20001 120 400
20002 61 130
20003 30 30
20004 30 30
20005 65 204
20006 152 429
20007 30 30

The solution is calculating on the simple_subtraction coluimn, but I believe it should be calculating on the datediff_calc column.

Thanks!

Will

Answers

Hi Will,

Good morning!

Thank you and you are absolutely righ! Just corrected the MySQL version of solution.

Have a great rest of the weekend!

Leon

SQLPad user avatar

Leon (949)

Jan. 29, 2023, 9:57 a.m.