Skip to main content
Last updated on

NANOSECONDS_ADD

Description

Adds a number of nanoseconds to a TIMESTAMP_NS value while preserving nanosecond precision.

Syntax

NANOSECONDS_ADD(<timestamp_ns>, <delta>)

Parameters

ParameterDescription
<timestamp_ns>The base value, of type TIMESTAMP_NS.
<delta>The number of nanoseconds to add, of type BIGINT. A negative value subtracts nanoseconds.

Return Value

Returns TIMESTAMP_NS. Returns NULL if any argument is NULL. Reports an error if the result is outside the TIMESTAMP_NS range.

Example

SELECT NANOSECONDS_ADD(
CAST('1969-12-31 23:59:59.999999999' AS TIMESTAMP_NS), 1
) AS result;
+-------------------------------+
| result |
+-------------------------------+
| 1970-01-01 00:00:00.000000000 |
+-------------------------------+

A negative delta subtracts nanoseconds:

SELECT NANOSECONDS_ADD(
CAST('1970-01-01 00:00:00.000000001' AS TIMESTAMP_NS), -2
) AS result;
+-------------------------------+
| result |
+-------------------------------+
| 1969-12-31 23:59:59.999999999 |
+-------------------------------+
SELECT NANOSECONDS_ADD(CAST(NULL AS TIMESTAMP_NS), 1) AS result;
+--------+
| result |
+--------+
| NULL |
+--------+