dateadd()
Returns a new date by adding or subtracting a specified number of time units from a date.
Syntax
dateadd(DATE, UNIT, DELTA)
Parameters
DATE- An expression that returns the base date.
UNIT- A string representing the time unit to add or subtract. The supported units are as follows.
| Unit | Description |
|---|---|
year | Years |
mon | Months |
day | Days |
hour | Hours |
min | Minutes |
sec | Seconds |
msec | Milliseconds |
DELTA- An integer value to add to or subtract from DATE. A positive value adds, a negative value subtracts.
Description
The dateadd() function returns a date by adding or subtracting DELTA in the specified UNIT from DATE. The return type is timestamp.
Returns null if DATE is null or not a timestamp type. Returns null if UNIT is invalid. Returns null if DELTA is not an integer.
Error codes
90620- Raised when the number of arguments is not 3.
90621- Raised when the UNIT value is invalid. The
fieldparameter contains the invalid unit. 90622- Raised when DELTA is a constant of non-integer type. The
timeparameter contains the value.
Usage examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Add 1 day to the access log timestamp.
table limit=5 WEB_APACHE_SAMPLE | eval result = dateadd(_time, "day", 1) | fields _time, result -
Subtract 3 hours from the access log timestamp.
table limit=5 WEB_APACHE_SAMPLE | eval result = dateadd(_time, "hour", -3) | fields _time, result -
NULL input
json "{}" | eval result = dateadd(null, "hour", 1) | # result: null
Compatibility
The dateadd() function has been available since before Sonar 4.0.