try_to_date function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 17.2 and above

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

Syntax

try_to_date(expr [, fmt] )

Arguments

  • expr: A STRING, DATE, or TIMESTAMP expression that represents a date.
  • fmt: An optional format STRING expression.

Returns

A DATE.

If you omit fmt, the function is equivalent to try_cast(expr AS DATE).

If fmt is malformed, Azure Databricks raises INVALID_DATETIME_PATTERN. If expr cannot be parsed using fmt, the function returns NULL.

Common error conditions

Examples

> SELECT try_to_date('2016-12-31');
  2016-12-31

> SELECT try_to_date('31/12/2016', 'dd/MM/yyyy');
  2016-12-31

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