Updated on 2026-11-06 GMT+08:00

datediff

The datediff function is used to calculate the date difference between two times, date1 and date2.

Similar function: datediff1. The datediff1 function calculates the difference between two times, date1 and date2, and expresses the difference in the specified time unit datepart.

Syntax

datediff(string date1, string date2)

Parameters

Table 1 Parameter descriptions

Parameter

Mandatory

Type

Description

date1

Yes

DATE

or

STRING

The minuend date.

Formats:

yyyy-mm-dd

yyyy-mm-dd hh:mi:ss

yyyy-mm-dd hh:mi:ss.ff3

date2

Yes

DATE

or

STRING

The subtrahend date.

Formats:

yyyy-mm-dd

yyyy-mm-dd hh:mi:ss

yyyy-mm-dd hh:mi:ss.ff3

Return Values

Returns a value of type BIGINT.

  • If date1 or date2 is not of type DATE or STRING, an error is returned with the message "data type mismatch".
  • If date1 or date2 is of type DATE or STRING but does not conform to the input format for date values, NULL is returned.
  • If date1 is less than date2, the return value is negative.
  • If the date1 or date2 value is NULL, NULL is returned.

Example Code

Returns 10.

select datediff('2023-06-30 00:00:00', '2023-06-20 00:00:00');

Returns 11.

select datediff(date '2023-05-21', date '2023-05-10');

Returns NULL.

select datediff(date '2023-05-21', null);