跳转到内容

计算两个时间戳之间的差值,并以 INTERVAL 形式返回结果。

func.timestamp_diff(<timestamp1>, <timestamp2>)
func.timestamp_diff(func.to_timestamp('2025-02-01'), func.to_timestamp('2025-01-01'))
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ func.timestamp_diff(func.to_timestamp('2025-02-01'), func.to_timestamp('2025-01-01')) │
├────────────────────────────────────────────────────────────────────────────────────────┤
│ 744:00:00 │
└────────────────────────────────────────────────────────────────────────────────────────┘
TIMESTAMP_DIFF(<timestamp1>, <timestamp2>)

INTERVAL(格式为 hours:minutes:seconds)。

此示例显示 2025 年 2 月 1 日与 2025 年 1 月 1 日之间的时间差为 744 小时,相当于 31 天:

SELECT TIMESTAMP_DIFF('2025-02-01'::TIMESTAMP, '2025-01-01'::TIMESTAMP);
┌──────────────────────────────────────────────────────────────────┐
│ timestamp_diff('2025-02-01'::TIMESTAMP, '2025-01-01'::TIMESTAMP) │
├──────────────────────────────────────────────────────────────────┤
│ 744:00:00 │
└──────────────────────────────────────────────────────────────────┘