Appearance
ADD_MONTHS
Add some number of months to a date or timestamp expression. (You can also subtract months by using a negative number for the second argument.)
ADD_MONTHS(expression, number)
For example:
premdb=# select matchday, add_months(matchday,6) from match where htid=2 and atid=60;
matchday | add_months
---------------------+---------------------
2010-03-06 00:00:00 | 2010-09-06 00:00:00
(1 row)
premdb=# select matchday, add_months(matchday,-6) from match where htid=2 and atid=60;
matchday | add_months
---------------------+---------------------
2010-03-06 00:00:00 | 2009-09-06 00:00:00
(1 row)
If the month in the result has fewer days than the day specified in the input expression, the function returns the last day of the month. In other cases, the result contains the same day of month number as the input expression. However, when the input expression is the last day of the month, the function always returns the last day of the result month. For example, in a leap year, such as 2000:
premdb=# select add_months('2000-02-28',1)::date from sys.const;
add_months
------------
2000-03-28
(1 row)
premdb=# select add_months('2000-02-29',1)::date from sys.const;
add_months
------------
2000-03-31
(1 row)
If either argument is null
, the result is null
.
Note: You cannot use the ADD_MONTHS
function in queries against system views.
Parent topic:Datetime Functions