Both functions measure the gap between two dates, but they do not agree on which date comes first. timestampdiff can return years, months, days and other units, while datediff only returns days. The real trap is the sign: in the China-domestically developed GBase 8a MPP database from GBASE, datediff computes date1 - date2, whereas timestampdiff computes date2 - date1.

DateDiff

date1 - date2. When the first date is later than the second, the result is positive.

gbase> select datediff(now(),'2004-05-01'); +------------------------------+ | datediff(now(),'2004-05-01') | +------------------------------+ | 6977 | +------------------------------+ 1 row in set (Elapsed: 00:00:00.01) 

TimestampDiff

date2 - date1. When the first date is later than the second, the result is negative — the same pair of dates as above returns -6977.

gbase> select timestampdiff(day,now(),'2004-05-01'); +---------------------------------------+ | timestampdiff(day,now(),'2004-05-01') | +---------------------------------------+ | -6977 | +---------------------------------------+ 

PERIOD_DIFF

date1 - date2, the same direction as datediff. It compares two period values, here 20230608 and 20230607, and returns 1.

gbase> select PERIOD_DIFF(20230608,20230607); +--------------------------------+ | PERIOD_DIFF(20230608,20230607) | +--------------------------------+ | 1 | +--------------------------------+ 1 row in set (Elapsed: 00:00:00.00) 

Going forward, do not carry an argument order from one date function to another in gbase SQL: write the two dates down, decide which subtraction you need, and check the sign of a known pair before trusting a production query. A single swapped sign is enough to invert every age or SLA calculation a gbase database returns.