In PostgreSQL, we can use the +
operator to add one or more seconds to a time value.
By “time” value, this could be an actual time
value, a timestamp
, or an interval
. We can also add seconds to a date
value or a date
and time
combination.
Example
We can specify intervals when adding to dates and times. So to add one or more seconds, we can use second
or seconds
:
SELECT time '07:00' + interval '1 second';
Result:
07:00:01
Plural
And in plural form:
SELECT time '07:00' + interval '45 seconds';
Result:
07:00:45
Timestamps
And here it is with a timestamp
value:
SELECT timestamp '2030-01-20 09:00' + interval '30 seconds';
Result:
2030-01-20 09:00:30
Intervals
We can also add seconds to an interval
:
SELECT interval '5 minutes' + interval '90 seconds';
Result:
00:06:30
Dates
We can even add seconds to a date
value:
SELECT date '2030-01-20' + interval '12 seconds';
Result:
2030-01-20 00:00:12
The result is a timestamp
value.
Date & Time Values Combined
We can also add a date
and time
value together, and add seconds to that:
SELECT date '2030-01-20' + time '01:00' + interval '18 seconds';
Result:
2030-01-20 01:00:18
Specified in Minutes
If the seconds to be added are in 60 second increments, we can alternatively use minutes:
SELECT time '15:45' + interval '1 minute';
Result:
15:46:00
Negative Values
It’s possible to perform date arithmetic with negative values. If we use a negative value with the +
operator, then the specified number of seconds will be subtracted from the input date/time. But if we use it with the -
operator, then it will be added to the input date/time.
Example:
SELECT time '03:00' - interval '-2 seconds';
Result:
03:00:02