to_date function

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

Returns expr cast to a date using an optional formatting.

Syntax

to_date(expr [, fmt] )

Arguments

  • expr: A STRING expression representing a date.

  • fmt: An optional format STRING expression.

Returns

A DATE.

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

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

If fmt is malformed or its application does not result in a well formed date, the function raises an error.

Note

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

Examples

> SELECT to_date('2009-07-30 04:17:52');
 2009-07-30
> SELECT to_date('2016-12-31', 'yyyy-MM-dd');
 2016-12-31