The MONTHS_SUB function is used to add or subtract a specified number of months to a given date and returns the resulting date.
MONTHS_SUB(<datetime/date>, <nums>)
| Parameter | Description |
|---|---|
<datetime/date> | The date value to which months will be added or subtracted |
<nums> | The number of months to add or subtract |
The return value is of the same type as the input <datetime/date>. Special cases:
select months_sub("2020-01-31 02:02:02", 1),months_sub("2020-01-31", 1),months_sub("2020-01-31", -1);
+-------------------------------------------------------------+---------------------------------------------+----------------------------------------------+ | months_sub(cast('2020-01-31 02:02:02' as DATETIMEV2(0)), 1) | months_sub(cast('2020-01-31' as DATEV2), 1) | months_sub(cast('2020-01-31' as DATEV2), -1) | +-------------------------------------------------------------+---------------------------------------------+----------------------------------------------+ | 2019-12-31 02:02:02 | 2019-12-31 | 2020-02-29 | +-------------------------------------------------------------+---------------------------------------------+----------------------------------------------+