label: MySQL, SQL Date Funcitons, DATE_ADD, DATE_SUB, ADDDATE, SUBDATE, INTERVAL, MySQL date function, MySQL date addition and subtraction
From a programming language perspective, date and time are relatively complex and advanced data types relative to integers and strings, and usage in various languages is less uniform.
This article mainly explains the functions in MySQL that add or will specify the time interval for the date and time. Addition uses DATE_ADD(), and subtraction uses DATE_SUB()
DATE_ADD and DATE_SUB
SELECT DATE_ADD('2019-11-11', INTERVAL 2 MONTH);
SELECT DATE_SUB('2019-11-11', INTERVAL 2 MONTH);
ADDDATE and SUBDATE
SELECT ADDDATE('2019-11-11', INTERVAL 2 MONTH);
SELECT SUBDATE('2019-11-11', INTERVAL 2 MONTH);
These two functions are identical to the DATE_ADD and DATE_SUB functions except that INTERVAL can be omitted.
Here are a few simple examples:
The INTERVAL 2 MONTH is the time interval, INTERVAL is a fixed character, 2 is the quantity, and MONTH is the interval type. All types are as follows:
It also supports more complex intervals, the corresponding format is as follows:
DAY_MICROSECOND 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
DAY_SECOND 'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE 'DAYS HOURS:MINUTES'
DAY_HOUR 'DAYS HOURS'