Skip to content

DATEADD

Add a specified interval of time to a date or timestamp value.

DATEADD( datepart, interval, { date | timestamp } )
datepart

See Supported Dateparts.

interval

An integer that defines the number of days, weeks, or months, for example, to add to the specified date or timestamp. A negative integer subtracts the interval. You cannot use an interval literal; the interval value must be an integer.

date | timestamp

The date or timestamp must contain the specified date part. Use the date or timestamp keyword to specify a literal value. For example:

date '2016-02-28'
timestamp '2016-02-28 19:34:12.123'

Note: All DATE values implicitly have a time of 00:00:00. Therefore, if you subtract a minute (or some other small amount of time) from a given date, the result is the previous day. On the other hand, if you add less than 24 hours to a date, the result is the same day.

Examples

For example, add 1 month to the matchday column:

premdb=# select seasonid, matchday, 
dateadd(month,1,matchday) matchday_plus_one 
from match where htid=5 and atid=52 order by 1;
 seasonid |      matchday       |  matchday_plus_one  
----------+---------------------+---------------------
      11 | 2002-09-16 00:00:00 | 2002-10-16 00:00:00
      12 | 2003-10-19 00:00:00 | 2003-11-19 00:00:00
      13 | 2005-03-20 00:00:00 | 2005-04-20 00:00:00
      14 | 2005-10-16 00:00:00 | 2005-11-16 00:00:00
      16 | 2007-11-11 00:00:00 | 2007-12-11 00:00:00
      18 | 2009-09-13 00:00:00 | 2009-10-13 00:00:00
      19 | 2011-01-16 00:00:00 | 2011-02-16 00:00:00
(7 rows)

This example adds 300 seconds to a given timestamp:

premdb=# select timestamp '2017-11-21 3:31:00', dateadd(seconds, 300, timestamp '2017-11-21 3:31:00') as plus300secs from sys.const;
     timestamp      |     plus300secs     
---------------------+---------------------
 2017-11-21 03:31:00 | 2017-11-21 03:36:00
(1 row)

Parent topic:Datetime Functions