Subtracts the specified time interval from a date/time or time expression. If the second parameter is negative, it is equivalent to adding the interval to the first parameter.
SUB_TIME(`<date_or_time_expr>`, `<time>`)
| Parameter | Description |
|---|---|
<date_or_time_expr> | A valid date expression. Supports input of timestamptz/datetime/date/time types. If the type is date, it will be converted to the start time of the day (00:00:00). For specific formats, see timestamptz conversion, datetime conversion and time conversion. |
<time> | A valid time expression, representing the time value to be subtracted from <date_or_time_expr>. If negative, it means addition. Supports input of time type. |
Returns the result of subtracting <time> from <date_or_time_expr>. The return type depends on the type of the first parameter:
Special cases:
-- Subtract time when the first parameter is datetime type SELECT SUB_TIME('2025-09-19 12:00:00', '01:30:00'); +---------------------------------------------+ | SUB_TIME('2025-09-19 12:00:00', '01:30:00') | +---------------------------------------------+ | 2025-09-19 10:30:00 | +---------------------------------------------+ -- Subtract time when the first parameter is time type SELECT SUB_TIME(cast('12:15:20' as time), '00:10:40'); +------------------------------------------------+ | SUB_TIME(cast('12:15:20' as time), '00:10:40') | +------------------------------------------------+ | 12:04:40 | +------------------------------------------------+ -- SET time_zone = '+08:00' select sub_time('2025-10-10 11:22:33+03:00', '3:22:33.123'); +------------------------------------------------------+ | sub_time('2025-10-10 11:22:33+03:00', '3:22:33.123') | +------------------------------------------------------+ | 2025-10-10 12:59:59.877+08:00 | +------------------------------------------------------+ -- NULL parameter test SELECT SUB_TIME(NULL, '01:00:00'); +----------------------------+ | SUB_TIME(NULL, '01:00:00') | +----------------------------+ | NULL | +----------------------------+ SELECT SUB_TIME('2025-09-19 12:00:00', NULL); +---------------------------------------+ | SUB_TIME('2025-09-19 12:00:00', NULL) | +---------------------------------------+ | NULL | +---------------------------------------+ SELECT SUB_TIME(NULL, NULL); +----------------------+ | SUB_TIME(NULL, NULL) | +----------------------+ | NULL | +----------------------+ -- Time type out-of-range test (returns max/min value) SELECT SUB_TIME(cast('835:30:00' as time), '-21:00:00'); +--------------------------------------------------+ | SUB_TIME(cast('835:30:00' as time), '-21:00:00') | +--------------------------------------------------+ | 838:59:59 | +--------------------------------------------------+ SELECT SUB_TIME(cast('-832:30:00' as time), '31:00:00'); +---------------------------------------------------+ | SUB_TIME(cast('-832:30:00' as time), '31:00:00') | +---------------------------------------------------+ | -838:59:59 | +---------------------------------------------------+ -- Datetime type out-of-range test (throws error) SELECT SUB_TIME('0000-01-01 00:00:00', '00:00:01'); ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[INVALID_ARGUMENT]datetime value is out of range in function sub_time SELECT SUB_TIME('9999-12-31 23:59:59', '-00:00:01'); ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[INVALID_ARGUMENT]datetime value is out of range in function sub_time