SECONDS_ADD
Description
The SECONDS_ADD function adds or subtracts a specified number of seconds to a specified datetime value and returns the calculated datetime value. This function supports processing DATE, DATETIME and TIMESTAMPTZ types. If a negative number is input, it is equivalent to subtracting the corresponding number of seconds.
This function is consistent with date_add function and MySQL's date_add function when using SECOND as the unit.
Syntax
SECONDS_ADD(<date_or_time_expr>, <seconds>)
Parameters
| Parameter | Description |
|---|---|
<date_or_time_expr> | Required. The input datetime value. Can be of type DATE, DATETIME or TIMESTAMPTZ. For specific formats, see timestamptz conversion, datetime conversion and date conversion |
<seconds> | Required. The number of seconds to add or subtract. Supports integer type (BIGINT). Positive numbers indicate adding seconds, negative numbers indicate subtracting seconds. |
Return Value
Return the base time <date_or_time_expr> by adding the specified number of seconds <second>, the return value type is determined by the type of the first parameter:
- If the type of the first parameter is DATE/DATETIME, then the return type is DATETIME.
- If the type of the first parameter is TIMESTAMPTZ, then the return type is TIMESTAMPTZ.
Special cases:
- If
<seconds>is negative, the function behaves the same as subtracting the corresponding seconds from the base time (i.e., SECONDS_ADD(date, -n) is equivalent to SECONDS_SUB(date, n)). - If the input is DATE type (only contains year, month, day), its time portion defaults to 00:00:00.
- If the calculation result exceeds the valid range of the date type (DATE type: 0000-01-01 to 9999-12-31; DATETIME/TIMESTAMPTZ type: 0000-01-01 00:00:00 to 9999-12-31 23:59:59.999999), returns an error.
- If any parameter is NULL, returns NULL.
Examples
--- Add seconds to DATETIME type
SELECT SECONDS_ADD('2025-01-23 12:34:56', 30) AS result;
+---------------------+
| result |
+---------------------+
| 2025-01-23 12:35:26 |
+---------------------+
--- Subtract seconds from DATETIME type (using negative number)
SELECT SECONDS_ADD('2025-01-23 12:34:56', -30) AS result;
+---------------------+
| result |
+---------------------+
| 2025-01-23 12:34:26 |
+---------------------+
--- Add seconds across minute boundary
SELECT SECONDS_ADD('2023-07-13 23:59:50', 15) AS result;
+---------------------+
| result |
+---------------------+
| 2023-07-14 00:00:05 |
+---------------------+
--- Input is DATE type (default time 00:00:00)
SELECT SECONDS_ADD('2023-01-01', 3600) AS result;
+---------------------+
| result |
+---------------------+
| 2023-01-01 01:00:00 |
+---------------------+
--- DATETIME with scale (preserves precision)
SELECT SECONDS_ADD('2023-07-13 10:30:25.123456', 2) AS result;
+----------------------------+
| result |
+----------------------------+
| 2023-07-13 10:30:27.123456 |
+----------------------------+
-- Example of TimeStampTz type, SET time_zone = '+08:00'
SELECT SECONDS_ADD('2025-10-10 11:22:33.123+07:00', 1);
+-------------------------------------------------+
| SECONDS_ADD('2025-10-10 11:22:33.123+07:00', 1) |
+-------------------------------------------------+
| 2025-10-10 12:22:34.123+08:00 |
+-------------------------------------------------+
--- Returns NULL when input is NULL
SELECT SECONDS_ADD(NULL, 30), SECONDS_ADD('2025-01-23 12:34:56', NULL) AS result;
+-------------------------+--------+
| seconds_add(NULL, 30) | result |
+-------------------------+--------+
| NULL | NULL |
+-------------------------+--------+
--- Calculation result exceeds date range
SELECT SECONDS_ADD('9999-12-31 23:59:59', 2) AS result;
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation seconds_add of 9999-12-31 23:59:59, 2 out of range
select seconds_add('0000-01-01 00:00:30',-31);
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation seconds_add of 0000-01-01 00:00:30, -31 out of range