MySQL SUBTIME() Function
MySQL SUBTIME() Function
The MySQL SUBTIME() function subtracts a TIME expression (expr2) from a TIME or DATETIME expression (expr1). It returns a value of the same type as expr1.
Teams use SUBTIME() to calculate clock-in times before a shift end, adjust schedules by hours and minutes together (like '01:45:00'), or subtract elapsed durations from timestamps.
Because expr2 accepts full TIME strings including days (such as '1 02:30:00' for 26 hours and 30 minutes), SUBTIME() handles multi-part time offsets in a single call.
Syntax
SUBTIME(expr1, expr2)
expr1: ATIMEorDATETIMEexpression.expr2: ATIMEexpression to subtract fromexpr1.
Practical Example
Suppose demo_employees stores shift end timestamps in shift_end (DATETIME) and scheduled break lengths in break_time (TIME). You can compute the net work cutoff with SUBTIME().
SELECT
employee_id,
shift_end,
break_time,
SUBTIME(shift_end, break_time) AS net_cutoff
FROM demo_employees
WHERE employee_id IN (11, 12);
| employee_id | shift_end | break_time | net_cutoff |
|---|---|---|---|
| 11 | 2026-05-10 17:30:00 | 00:45:00 | 2026-05-10 16:45:00 |
| 12 | 2026-05-10 00:30:00 | 01:00:00 | 2026-05-09 23:30:00 |
For employee 12, subtracting 01:00:00 crosses midnight into '2026-05-09 23:30:00' because expr1 is a DATETIME.
Common Mistakes & NULL Behavior
Do not pass a pure DATE value (without a time part) as expr1. SUBTIME('2026-05-10', '01:00:00') fails TIME/DATETIME validation and returns NULL! Use SUBDATE() or TIMESTAMP(date_col) first when starting from a DATE.
Watch the return type when expr1 is TIME. Subtracting '02:00:00' from '01:00:00' produces negative TIME '-01:00:00', whereas subtracting from a DATETIME rolls back to the previous calendar day.
Also remember TIME outputs are clamped between -838:59:59 and 838:59:59. SUBTIME() returns NULL if either argument is NULL or cannot be parsed as a valid TIME/DATETIME.
Frequently Asked Questions
What is the difference between SUBTIME() and TIMEDIFF()?
SUBTIME(expr1, expr2) subtracts a TIME duration from a TIME or DATETIME and preserves expr1's type. TIMEDIFF(expr1, expr2) finds the difference between two matching TIME or two DATETIME values and always returns TIME.
Can expr2 in SUBTIME() include days?
Yes. Passing '2 03:00:00' as expr2 subtracts 2 days and 3 hours (51:00:00).
Why did SUBTIME('2026-05-10', '00:30:00') return NULL?
SUBTIME() requires expr1 to be a TIME or DATETIME. Wrap the date with TIMESTAMP('2026-05-10') or use DATE_SUB() instead.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.