SUBTIME in MySQL

The SUBTIME() function subtracts a given time from a specified time or DateTime expression and then returns the resulting time/DateTime.

Syntax:
SUBTIME(datetime, time_interval)
Note that both parameters are required. The time interval is the one that gets subtracted from datetime and can be either a positive or a negative value.

Examples:
SELECT SUBTIME(“2023-04-23 10:18:18.000021”, “5.000001”);
Result: 2023-04-23 10:18:13.000020

SELECT SUBTIME(“15:04:18”, “5”);
Result: 15:04:13

SELECT SUBTIME(“15:04:18”, “300”);
300 means 3 minutes.
Result:
15:01:18

SELECT SUBTIME(“15:04:18”, “00:05:00”);
“00:05:00” means 5 minutes.
Result: 14:59:18

SELECT SUBTIME(“15:04:18”, “5:5:5”);
“-5:5:5” means SUBTRACTING 5 hours 5 minutes and 5 seconds to the given date.
Result: 09:59:13

SELECT SUBTIME(“15:04:18”, “-5:5:5”);
“-5:5:5” means ADDING 5 hours 5 minutes and 5 seconds to the given date.
Result: 20:09:23

Hope you find this article helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s