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

# `try_to_timestamp` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 11.3 LTS and above

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

## Syntax

```
try_to_timestamp(expr [, fmt] )
```

## Arguments

* `expr`: A `STRING`, `DATE`, `TIMESTAMP`, or numeric expression that represents a timestamp.
* [fmt](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datetime-pattern.md): An optional format `STRING` expression.

## Returns

A `TIMESTAMP`.

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

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

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

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

## Common error conditions

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

## Examples

```sql
> SELECT try_to_timestamp('2016-12-31 00:12:00');
 2016-12-31 00:12:00

> SELECT try_to_timestamp('2016-12-31', 'yyyy-MM-dd');
 2016-12-31 00:00:00

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

> SELECT to_timestamp('2016-12-32', 'yyyy-MM-dd');
 Error: CANNOT_PARSE_TIMESTAMP
```

## Related functions

* [`cast` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/cast.md)
* [`timestamp` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/timestamp.md)
* [`to_date` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_date.md)
* [`to_timestamp` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_timestamp.md)
* [`to_timestamp_ltz` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_timestamp_ltz.md)
* [`to_timestamp_ntz` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_timestamp_ntz.md)
* [Datetime patterns](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datetime-pattern.md)
