Note
This function is available starting from OceanBase Database V4.6.0.
Syntax
MONTHS_ADD(date, months)
Purpose
This function adds the specified number of months to the date date.
Supported data types
The months_add function in OceanBase Database supports various date-related data types as input and returns the corresponding type based on the input type. The mapping is as follows:
- Input
DATEreturnsDATE. - Input
DATETIMEreturnsDATETIME. - Input
TIMESTAMPreturnsDATETIME. - Input
VARCHAR(a string that can be parsed as a date) returnsDATETIME. - Input
TIMEreturnsDATETIME. ForTIMEtype, the function uses the current real-time date for the year, month, and day, and then calculates with the given number of months.
Behavior
- In OceanBase Database, the
month_addfunction returnsNULLinstead of throwing an error when the input is invalid. - It can handle
TIMEtype, using the current real-time date for the year, month, and day. - It allows invalid dates to be calculated based on the
ALLOW_INVALID_DATESsetting. - When the parameter is a string in a single
SELECTstatement, it is uniformly converted toDATETIMEtype for output.
Examples
Add 1 month to 2026-01-31.
obclient> SELECT MONTHS_ADD('2026-01-31', 1);
The result is as follows:
+-----------------------------+
| MONTHS_ADD('2026-01-31', 1) |
+-----------------------------+
| 2026-02-28 00:00:00 |
+-----------------------------+
1 row in set
