---
title: try_to_time function
description: Learn the syntax of the try\_to\_time function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/try_to_time
---

# `try_to_time` function

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 18 LTS 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](https://docs.databricks.com/gcp/en/release-notes/runtime/18.md#unified-format).

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

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](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-datetime-pattern.md): An optional format STRING expression.

## Returns

A [TIME](https://docs.databricks.com/gcp/en/sql/language-manual/data-types/time-type.md).

If `fmt` is supplied, it must conform with [Datetime patterns](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-datetime-pattern.md).

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

If `fmt` is malformed, Databricks raises [INVALID\_DATETIME\_PATTERN](https://docs.databricks.com/gcp/en/error-messages/invalid-datetime-pattern-error-class.md).

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

## Common error conditions

* [INVALID\_DATETIME\_PATTERN](https://docs.databricks.com/gcp/en/error-messages/invalid-datetime-pattern-error-class.md)

## Examples

```sql
> 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
```

## Related functions

* [`time` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/time.md)
* [`to_time` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/to_time.md)
* [`cast` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/cast.md)
* [`try_to_timestamp` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/try_to_timestamp.md)
* [`TIME` type](https://docs.databricks.com/gcp/en/sql/language-manual/data-types/time-type.md)
* [Datetime patterns](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-datetime-pattern.md)
