更新时间:2026-06-11 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函数用于计算两个日期之间的天数差值。
约束与限制
本章节仅适用于MRS 3.6.0-LTS.1及之后版本。
入参
| 参数 | 数据类型 |
|---|---|
| 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-9-15 00:00:00') as int1,
DATEDIFF(time1 ,'2025-12-1 00:00:00') as int2,
DATEDIFF('2025-12-1',time1 ) as int3,
DATEDIFF(time1 ,CAST(null as VARCHAR)) as int4,
DATEDIFF(CAST(null as VARCHAR), '2025-12-1 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 |
父主题: FlinkSQL 内置函数