The SUBTIME() function in MySQL is used to subtract a time interval from a time or datetime value. It returns a new value after subtracting the specified time interval.
Syntax:
SUBTIME(time, expr)
time: A time or datetime value from which the subtraction will be performed.expr: A time or datetime interval that will be subtracted from thetimevalue. It can be in formats such as'HH:MM:SS','HH:MM','HH', or a datetime expression.
Example 1: Subtracting time from a datetime value
SELECT SUBTIME('2024-12-23 10:00:00', '00:30:00');
Result:
2024-12-23 09:30:00
Here, 30 minutes are subtracted from the 2024-12-23 10:00:00 value.
Example 2: Subtracting time from a TIME value
SELECT SUBTIME('12:45:00', '01:30:00');
Result:
11:15:00
Here, 1 hour and 30 minutes are subtracted from 12:45:00.
Example 3: Subtracting an interval from CURRENT_TIMESTAMP
SELECT SUBTIME(CURRENT_TIMESTAMP, '1 10:00:00');
Result:
2024-12-22 00:00:00
In this case, 1 day and 10 hours are subtracted from the current timestamp.
Notes:
- If
timeandexprare in different units, MySQL will automatically convert them. - The function can also handle negative intervals properly.
No comments:
Post a Comment