to_timestamp function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Returns expr cast to a timestamp using an optional formatting.

Syntax

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 or its application does not result in a well formed timestamp, the function raises an error.

To return NULL instead of an error in case of a malformed expr use try_to_timestamp.

Note

In Databricks Runtime, if spark.sql.ansi.enabled is false, the function returns NULL instead of an error for malformed timestamps.

Examples

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

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