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

DATEDIFF

Syntax

INT DATEDIFF(VARCHAR enddate, VARCHAR startdate)
INT DATEDIFF(TIMESTAMP enddate, VARCHAR startdate)
INT DATEDIFF(VARCHAR enddate, TIMESTAMP startdate)
INT DATEDIFF(TIMESTAMP enddate, TIMESTAMP startdate)

Description

The DATEDIFF function is used to calculate the number of days between two dates. The calculation subtracts startdate from enddate, meaning the return value equals enddate – startdate. A positive number is returned if enddate is later than startdate, and a negative number is returned if enddate is earlier than startdate.

Input Parameters

Parameter

Data Type

enddate

TIMESTAMP and VARCHAR

startdate

TIMESTAMP and VARCHAR

  • VARCHAR date format: yyyy-MM-dd or yyyy-MM-dd HH:mm:ss
  • If any input parameter is NULL, NULL is returned.

Example

CREATE TABLE source (
  time1 TIMESTAMP
) WITH (
  'connector' = 'datagen',
  'rows-per-second' = '1'
);
CREATE TABLE print with(
'connector' = 'print'
) as select 
DATEDIFF('2025-10-15 00:00:00', '2025-09-15 00:00:00') as int1, 
        DATEDIFF(time1 ,'2025-12-01 00:00:00') as int2, 
        DATEDIFF('2025-12-01',time1 ) as int3, 
        DATEDIFF(time1 ,CAST(null as VARCHAR)) as int4, 
        DATEDIFF(CAST(null as VARCHAR), '2025-12-01 00:00:00') as int5, 
        DATEDIFF(CAST(null as VARCHAR), time1 ) as int6, 
        DATEDIFF(time1 ,time1 ) as int7
from source;
Table 1 Returned Result

int1(INT)

int2(INT)

int3(INT)

int4(INT)

int5(INT)

int6(INT)

int7(INT)

30

49

-49

null

null

null

0