Adds a specified amount to one part of a datetime value, and returns a date in the same format as the date you pass to the function. The addToDate function accepts positive and negative integer values. Use addToDate to change the following parts of a date:
•Year. Enter a positive or negative integer in the amount argument. Use any of the year format strings: Y, YY, YYY, or YYYY. The following expression adds 10 years to all dates in the SHIP_DATE column:
•Month. Enter a positive or negative integer in the amount argument. Use any of the month format strings: MM, MON, MONTH. The following expression subtracts 10 months from each date in the SHIP_DATE column:
•Day. Enter a positive or negative integer in the amount argument. Use any of the day format strings: D, DD, DDD, DY, and DAY. The following expression adds 10 days to each date in the SHIP_DATE column:
•Hour. Enter a positive or negative integer in the amount argument. Use any of the hour format strings: HH, HH12, HH24. The following expression adds 14 hours to each date in the SHIP_DATE column:
•Minute. Enter a positive or negative integer in the amount argument. Use the MI format string to set the minute. The following expression adds 25 minutes to each date in the SHIP_DATE column:
•Seconds. Enter a positive or negative integer in the amount argument. Use the SS format string to set the second. The following expression adds 59 seconds to each date in the SHIP_DATE column:
•Milliseconds. Enter a positive or negative integer in the amount argument. Use the MS format string to set the milliseconds. The following expression adds 125 milliseconds to each date in the SHIP_DATE column:
•Microseconds. Enter a positive or negative integer in the amount argument. Use the US format string to set the microseconds. The following expression adds 2,000 microseconds to each date in the SHIP_DATE column:
Note: You must manually add the xs:dateTime phrase and enclose the date values within single quotation marks.
The following table describes the arguments:
Argument
Required/ Optional
Description
date
Required
Date/Time data type. Passes the values that you want to change.
You can enter any valid transformation expression.
format
Required
A format string that specifies the portion of the date value that you want to change. Enclose the format string within single quotation marks, for example, 'mm'. The format string is not case sensitive.
amount
Required
An integer value that specifies the amount of years, months, days, hours, and so on by which you want to change the date value. You can enter any valid transformation expression that evaluates to an integer.
Return Value
Date in the same format as the date you pass to this function.
NULL if a null value is passed as an argument to the function.
Examples
The following expressions all add one month to each date in the DATE_SHIPPED column. If you pass a value that creates a day that does not exist in a particular month, addToDate returns the last day of the month. For example, if you add one month to Jan 31 1998, addToDate returns Feb 28 1998.
Also, addToDate recognizes leap years and adds one month to Jan 29 2000: