try_to_date function
Applies to: Databricks SQL
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: ASTRING,DATE, orTIMESTAMPexpression that represents a date.- fmt: An optional format
STRINGexpression.
Returns
A DATE.
If you omit fmt, the function is equivalent to try_cast(expr AS DATE).
If fmt is malformed, Databricks raises INVALID_DATETIME_PATTERN. If expr cannot be parsed using fmt, the function returns NULL.
Common error conditions
Examples
SQL
> 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