更新时间:2026-11-06 GMT+08:00
DATEDIFF
语法
INT DATEDIFF(VARCHAR enddate, VARCHAR startdate) INT DATEDIFF(TIMESTAMP enddate, VARCHAR startdate) INT DATEDIFF(VARCHAR enddate, TIMESTAMP startdate) INT DATEDIFF(TIMESTAMP enddate, TIMESTAMP startdate)
描述
DATEDIFF函数用于计算两个日期之间的天数差值。计算方式为enddate减去startdate,即返回值 = enddate - startdate。当enddate晚于startdate时返回正数,当enddate早于startdate时返回负数。
入参
| 参数 | 数据类型 |
|---|---|
| enddate | TIMESTAMP、VARCHAR |
| startdate | TIMESTAMP、VARCHAR |
- VARCHAR日期格式:yyyy-MM-dd或yyyy-MM-dd HH:mm:ss。
- 如果入参中任何一个为NULL,则返回为NULL。
示例
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; | int1(INT) | int2(INT) | int3(INT) | int4(INT) | int5(INT) | int6(INT) | int7(INT) |
|---|---|---|---|---|---|---|
| 30 | 49 | -49 | null | null | null | 0 |
父主题: 增强函数说明