try_to_time function

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

Note

Databricks Runtime 18 is newer than Databricks Runtime 18.0, 18.1, and 18.2. Features that would previously have shipped as a later numbered version now ship as dated updates to Databricks Runtime 18 instead. For details, see About unified release notes.

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

Syntax

try_to_time(expr [, fmt] )

Arguments

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

Returns

A TIME.

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

If fmt is not supplied, the function is equivalent to cast(expr AS TIME).

If fmt is malformed, Azure Databricks raises INVALID_DATETIME_PATTERN.

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

Common error conditions

Examples

> SELECT try_to_time('10:30:00');
  10:30:00

> SELECT try_to_time('10:30:00', 'HH:mm:ss');
  10:30:00

> SELECT try_to_time('not-a-time');
  NULL

> SELECT try_to_time('10:30:00', 'HH:mm:ss') IS NOT NULL;
  true