7-2. Adding Some Number of Days to a Date

Problem

You want to add a number of days to a given date. For example, you are developing an application that calculates shipping dates for a company's products. In this case, your application is processing shipments, and you need to calculate a date that is 14 days from the current date.

Solution

Treat the number of days as an integer, and add that integer to your DATE value. The following lines of code show how this can be done:

DECLARE   ship_date    DATE := SYSDATE + 14; BEGIN   DBMS_OUTPUT.PUT_LINE('The shipping date for any products '||                        'that are ordered today is ' || ship_date); END;

The result that is displayed for this example will be 14 days past your current date.

Get Oracle and PL/SQL Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.