What is DATEDIFF() in SQL?

The DATEDIFF() function returns the number of days between two dates sent as parameter.

Figure 1 shows a visual representation of the DATEDIFF() function.

Figure 1: Visual representation of DATEDIFF() function

Syntax

DATEDIFF(date-1, date-2)

Parameter

The DATEDIFF() function takes two dates as a parameter.

Dates must be in the format YYYY-MM-DD, or this function returns NULL.

Return value

The DATEDIFF() function returns the number of days between two dates sent as a parameter.

This function only uses the date portion if the datetime is sent instead of date.

Example

-- date1>date2
SELECT DATEDIFF('2021-07-10','2021-07-09');
-- date1<date2
SELECT DATEDIFF('2021-07-10','2021-07-12');
-- datetime
SELECT DATEDIFF('2021-07-10 07:15:11','2021-07-09 09:24:31');
-- date in wrong format
SELECT DATEDIFF('10-07-2021','09-07-2021');

Free Resources