Date/time arithmetic, such as adding one or more minutes to a date/time value, is an operation we often need to perform when using SQL databases such as DuckDB. As with most RDBMSs, DuckDB makes this easy for us to achieve.
Here are two options for adding one or more minutes to a date, timestamp, or time value.
Option 1: The +
Operator
One way to add minutes to a date/time value in DuckDB is with the +
operator. This is basically like adding two numbers together, except we’re adding an interval to a date/time value.
Here’s an example of adding 15 minutes to a TIME
value, a TIMESTAMP
, and a DATE
value:
SELECT
TIME '04:30:05' + INTERVAL 15 MINUTE AS time,
TIMESTAMP '2040-01-01 04:30:05' + INTERVAL 15 MINUTE AS timestamp,
DATE '2040-01-01' + INTERVAL 15 MINUTE AS date;
Result:
+----------+---------------------+---------------------+
| time | timestamp | date |
+----------+---------------------+---------------------+
| 04:45:05 | 2040-01-01 04:45:05 | 2040-01-01 00:15:00 |
+----------+---------------------+---------------------+
When we add a time interval like minutes to a DATE
value, the result is a TIMESTAMP
value.
We can also use the plural form MINUTES
instead of MINUTE
:
SELECT
TIME '04:30:05' + INTERVAL 15 MINUTES AS time,
TIMESTAMP '2040-01-01 04:30:05' + INTERVAL 15 MINUTES AS timestamp,
DATE '2040-01-01' + INTERVAL 15 MINUTES AS date;
Result:
+----------+---------------------+---------------------+
| time | timestamp | date |
+----------+---------------------+---------------------+
| 04:45:05 | 2040-01-01 04:45:05 | 2040-01-01 00:15:00 |
+----------+---------------------+---------------------+
We can also add more than just minutes:
SELECT
TIME '04:30:05' + INTERVAL '3 HOURS 5 MINUTES 20 SECONDS' AS time,
TIMESTAMP '2040-01-01 04:30:05' + INTERVAL '3 HOURS 5 MINUTES 20 SECONDS' AS timestamp,
DATE '2040-01-01' + INTERVAL '3 HOURS 5 MINUTES 20 SECONDS' AS date;
Result:
+----------+---------------------+---------------------+
| time | timestamp | date |
+----------+---------------------+---------------------+
| 07:35:25 | 2040-01-01 07:35:25 | 2040-01-01 03:05:20 |
+----------+---------------------+---------------------+
When we do this, we need to pass the interval as a string (i.e. enclosed in single quotes).
Option 2: The DATE_ADD()
Function
Alternatively, we can use the date_add()
function to do the same thing that we did with the +
operator:
SELECT
date_add( TIME '04:30:05', INTERVAL 15 MINUTE ) AS time,
date_add( TIMESTAMP '2040-01-01 04:30:05', INTERVAL 15 MINUTE ) AS timestamp,
date_add( DATE '2040-01-01', INTERVAL 15 MINUTE ) AS date;
Result:
+----------+---------------------+---------------------+
| time | timestamp | date |
+----------+---------------------+---------------------+
| 04:45:05 | 2040-01-01 04:45:05 | 2040-01-01 00:15:00 |
+----------+---------------------+---------------------+
We can replace MINUTE
with MINUTES
to get the same result:
SELECT
date_add( TIME '04:30:05', INTERVAL 15 MINUTES ) AS time,
date_add( TIMESTAMP '2040-01-01 04:30:05', INTERVAL 15 MINUTES ) AS timestamp,
date_add( DATE '2040-01-01', INTERVAL 15 MINUTES ) AS date;
Result:
+----------+---------------------+---------------------+
| time | timestamp | date |
+----------+---------------------+---------------------+
| 04:45:05 | 2040-01-01 04:45:05 | 2040-01-01 00:15:00 |
+----------+---------------------+---------------------+
We can also add more than just minutes, just like with the +
operator:
SELECT
date_add( TIME '04:30:05', INTERVAL '3 HOURS 5 MINUTES 25 SECONDS' ) AS time,
date_add( TIMESTAMP '2040-01-01 04:30:05', INTERVAL '3 HOURS 5 MINUTES 25 SECONDS' ) AS timestamp,
date_add( DATE '2040-01-01', INTERVAL '3 HOURS 5 MINUTES 25 SECONDS' ) AS date;
Result:
+----------+---------------------+---------------------+
| time | timestamp | date |
+----------+---------------------+---------------------+
| 07:35:30 | 2040-01-01 07:35:30 | 2040-01-01 03:05:25 |
+----------+---------------------+---------------------+