Categories:
Conversion Functions

TRY_TO_TIMESTAMP / TRY_TO_TIMESTAMP_*

A special version of TO_TIMESTAMP / TO_TIMESTAMP_* that performs the same operation (i.e. converts an input expression into a timestamp), but with error-handling support (i.e. if the conversion cannot be performed, it returns a NULL value instead of raising an error).

For more information, see Error-handling Conversion Functions.

Note

TRY_TO_TIMESTAMP maps to one of the other timestamp functions, based on the TIMESTAMP_TYPE_MAPPING session parameter. The parameter default is TIMESTAMP_NTZ so TRY_TO_TIMESTAMP maps to TRY_TO_TIMESTAMP_NTZ by default.

Syntax

timestampFunction ( <string_expr> )

Where:

timestampFunction ::=
    TRY_TO_TIMESTAMP | TRY_TO_TIMESTAMP_LTZ | TRY_TO_TIMESTAMP_NTZ | TRY_TO_TIMESTAMP_TZ

Arguments

string_expr
The argument should be a string that can be evaluated to a TIMESTAMP (TIMESTAMP_NTZ, TIMESTAMP_LTZ, or TIMESTAMP_TZ).

Returns

The data type of the returned value is one of the TIMESTAMP data types. By default, the data type is TIMESTAMP_NTZ. You can change this by setting the session parameter TIMESTAMP_TYPE_MAPPING.

Usage Notes

Examples

This demonstrates usage of TRY_TO_TIMESTAMP:

SELECT TRY_TO_TIMESTAMP('2018-09-15 12:30:00'), TRY_TO_TIMESTAMP('Invalid');
+-----------------------------------------+-----------------------------+
| TRY_TO_TIMESTAMP('2018-09-15 12:30:00') | TRY_TO_TIMESTAMP('INVALID') |
|-----------------------------------------+-----------------------------|
| 2018-09-15 12:30:00.000                 | NULL                        |
+-----------------------------------------+-----------------------------+