In Oracle SQL, the ADD_MONTHS function is used to add a specified number of months to a date. It's a convenient way to perform date arithmetic in Oracle.
Here's the syntax for the ADD_MONTHS function in Oracle:
ADD_MONTHS(date, months_to_add)
- date: This is the date to which you want to add months.
- months_to_add: This is the number of months to add to the date. It can be positive (to add months) or negative (to subtract months).
Let's see an example to understand how ADD_MONTHS works in Oracle:
Suppose we have a date '2022-01-15', and we want to add 3 months to it.
SELECT ADD_MONTHS(DATE '2022-01-15', 3) AS new_date FROM dual;
This query will return the result:
NEW_DATE
2022-04-15
In this example, the ADD_MONTHS function adds 3 months to the date '2022-01-15', resulting in '2022-04-15'.
You can also use negative values with ADD_MONTHS to subtract months from a date. For example, to subtract 6 months from a date:
SELECT ADD_MONTHS(DATE '2022-01-15', -6) AS new_date FROM dual;
This would result in '2021-07-15'.
No comments:
Post a Comment