---
title: to_unix_timestamp function
description: Learn the syntax of the to\_unix\_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_unix_timestamp
---

# `to_unix_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 the timestamp in `expr` as a UNIX timestamp.

## Syntax

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

## Arguments

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

## Returns

A BIGINT.

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 [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.

## Common error conditions

* [CANNOT\_PARSE\_TIMESTAMP](https://docs.databricks.com/aws/en/error-messages/error-classes.md#cannot_parse_timestamp)

## Examples

```sql
> SELECT to_unix_timestamp('2016-04-08', 'yyyy-MM-dd');
 1460098800

> SELECT to_unix_timestamp('not-a-timestamp', 'yyyy-MM-dd');
  Error: CANNOT_PARSE_TIMESTAMP
```

## Related functions

* [`from_unixtime` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/from_unixtime.md)
* [Datetime patterns](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datetime-pattern.md)
