try_to_timestamp function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 11.3 LTS and above

Returns expr cast to a timestamp using an optional formatting, or NULL if the cast fails.

Syntax

try_to_timestamp(expr [, fmt] )

Arguments

  • expr: A STRING expression representing a timestamp.
  • fmt: An optional format STRING expression.

Returns

A TIMESTAMP.

If fmt is supplied, it must conform with Datetime patterns.

If fmt is not supplied, the function is a synonym for cast(expr AS TIMESTAMP).

If fmt is malformed, the function raises an error.

If expr cannot be transformed into a timestamp using fmt, the function returns NULL.

Examples

> SELECT try_to_timestamp('2016-12-31 00:12:00');
 2016-12-31 00:12:00

> SELECT try_to_timestamp('2016-12-31', 'yyyy-MM-dd');
 2016-12-31 00:00:00

> SELECT try_to_timestamp('2016-12-32', 'yyyy-MM-dd');
 NULL

> SELECT to_timestamp('2016-12-32', 'yyyy-MM-dd');
 Error: CANNOT_PARSE_TIMESTAMP