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

datediff1

The datediff1 function is used to calculate the difference between two times, date1 and date2, and expresses the difference in the specified time unit datepart.

Similar function: datediff. The datediff function calculates the date difference between two times, date1 and date2, and does not support specifying the return time unit.

Syntax

datediff1(string date1, string date2, string datepart)

Parameters

Table 1 Parameter descriptions

Parameter

Mandatory

Type

Description

date1

Yes

DATE

or

STRING

The minuend in the date difference calculation between two times, date1 and date2.

Formats:

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

date2

Yes

DATE

or

STRING

The subtrahend in the date difference calculation between two times, date1 and date2.

Formats:

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

datepart

Yes

STRING

The time unit to return.

The datepart parameter supports extended date formats:

year, month or mon, day, and hour.

  • yyyy indicates year.
  • mm indicates month.
  • dd indicates day.
  • hh indicates hour.
  • mi indicates minute.
  • ss indicates second.

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.
  • If the datepart value is NULL, NULL is returned.

Example Code

Returns 14400.

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

Returns 10.

select datediff1(date '2023-06-21', date '2023-06-11', 'dd');

Returns NULL.

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

Returns NULL.

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