---
title: to_timestamp function
description: Learn the syntax of the 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/to_timestamp
---

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

Returns `expr` cast to a timestamp using an optional formatting.

## Syntax

```
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 or its application does not result in a well formed timestamp, Databricks raises an error.

To return `NULL` instead of an error in case of a malformed `expr` use [try\_to\_timestamp](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_to_timestamp.md).

## Common error conditions

* [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md)
* [CANNOT\_PARSE\_TIMESTAMP](https://docs.databricks.com/aws/en/error-messages/error-classes.md#cannot_parse_timestamp)

> [!NOTE]
>
> In Databricks Runtime, if [spark.sql.ansi.enabled](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-ansi-compliance.md) is `false`, the function returns `NULL` instead of an error for malformed timestamps.

## Examples

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

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

> SELECT to_timestamp('not-a-timestamp');
  Error: CAST_INVALID_INPUT
```

## 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_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)
* [`try_to_timestamp` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_to_timestamp.md)
* [`to_date` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_date.md)
* [Datetime patterns](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datetime-pattern.md)
