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.
UnitDescription
yearYears
monMonths
dayDays
hourHours
minMinutes
secSeconds
msecMilliseconds
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 field parameter contains the invalid unit.
90622
Raised when DELTA is a constant of non-integer type. The time parameter contains the value.

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Add 1 day to the access log timestamp.

    table limit=5 WEB_APACHE_SAMPLE | eval result = dateadd(_time, "day", 1)
    | fields _time, result
    
  2. Subtract 3 hours from the access log timestamp.

    table limit=5 WEB_APACHE_SAMPLE | eval result = dateadd(_time, "hour", -3)
    | fields _time, result
    
  3. NULL input

    json "{}" | eval result = dateadd(null, "hour", 1)
    | # result: null
    

Compatibility

The dateadd() function has been available since before Sonar 4.0.