Smart SQL Tools for Every Database

MySQL Function Reference

MySQL DATEDIFF() Function

Learn how to use the MySQL DATEDIFF date function with practical examples and cross-dialect alternatives.

Back to SQL Function Explorer

Syntax

DATEDIFF(date1, date2)

DATEDIFF returns date1 - date2 in days. The result is an integer and can be negative.

Basic Example

SELECT DATEDIFF('2026-05-17', '2026-05-10') AS days_between;
-- Result: 7

Signed results (order matters)

SELECT DATEDIFF('2026-05-10', '2026-05-17') AS signed_days;
-- Result: -7

Practical query example

SELECT order_id, order_date
FROM orders
WHERE DATEDIFF(CURDATE(), order_date) > 30;

DATEDIFF vs TIMESTAMPDIFF

SELECT TIMESTAMPDIFF(HOUR, '2026-05-10 10:00:00', '2026-05-11 12:00:00') AS hours_between;
-- Result: 26

When to use it

Use DATEDIFF for retention windows, overdue checks, SLA aging buckets, and simple date-gap reporting in MySQL.

Common mistakes

Cross-dialect alternatives

Troubleshoot MySQL DATEDIFF problems

Related SQL Functions

FAQ

Does MySQL DATEDIFF include time-of-day?

No. It compares the date portion only and returns whole days.

Why is my result negative?

The function computes date1 - date2. Swap arguments if you want a positive value.

Can I use DATEDIFF in WHERE clauses?

Yes, but for large tables you should test index-friendly alternatives if performance drops.