SQL DATEDIFF Function

Quick summary: The SQL DATEDIFF() function returns the number of days between two dates.

SQL DATEDIFF Syntax

date1 - date2
SQL

SQL DATEDIFF Basic examples

SELECT DATEDIFF('2026-02-10', '2026-02-01');
SQL
Output:
9

Calculates the difference in days.

SQL DATEDIFF Real-world usage

SELECT DATEDIFF(NOW(), created_at) FROM users;
SQL
Output:
Account age in days

Measures time since creation.

SQL DATEDIFF Edge cases

SELECT DATEDIFF('2026-02-01', '2026-02-10');
SQL
Output:
-9

Negative values indicate reversed order.

SQL DATEDIFF Common mistakes

Assuming DATEDIFF includes time

DATEDIFF ignores time portions.

Incorrect
DATEDIFF(NOW(), created_at)
Correct
TIMESTAMPDIFF(HOUR, created_at, NOW())

Use TIMESTAMPDIFF for time precision.

SQL DATEDIFF Frequently Asked Questions

What does DATEDIFF() do in SQL?

Returns the difference between two dates.

Use case of DATEDIFF()?

Calculating date intervals.

Syntax?

DATEDIFF(date1, date2).

Return type?

Integer.

Common mistake?

Wrong parameter order.

Units?

Days (usually).

Handles NULL?

Returns NULL.

Alternative?

TIMESTAMPDIFF.

Used in WHERE?

Yes.

Supports time?

Depends on DB.

Performance?

Fast.

Best practice?

Be explicit with date order.

SQL DATEDIFF Related SQL Keywords