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

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

Casts the value `expr` to the target data type `type`. This operator is a synonym for [`::` (colon colon sign) operator](https://docs.databricks.com/aws/en/sql/language-manual/functions/coloncolonsign.md)

## Syntax

```
cast(sourceExpr AS targetType)
```

## Arguments

* `sourceExpr`: Any castable expression.
* `targetType`: The data type of the result.

## Returns

The result is type `targetType`.

The following combinations of data type casting are valid:

| Source (row) Target(column)                                                                                     | [VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md) | [numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification) | [STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md) | [DATE](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md) | [TIME](https://docs.databricks.com/aws/en/sql/language-manual/data-types/time-type.md) | [TIMESTAMP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md) | [TIMESTAMP\_NTZ](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md) | [year-month interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md) | [day-time interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md) | [BOOLEAN](https://docs.databricks.com/aws/en/sql/language-manual/data-types/boolean-type.md) | [BINARY](https://docs.databricks.com/aws/en/sql/language-manual/data-types/binary-type.md) | [ARRAY](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md) | [MAP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md) | [STRUCT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/struct-type.md) | [VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md) | [OBJECT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/object-type.md) |
| --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
| [VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)                          | Y                                                                                      | Y                                                                                                               | Y                                                                                          | Y                                                                                      | Y                                                                                      | Y                                                                                                | Y                                                                                                         | Y                                                                                                         | Y                                                                                                       | Y                                                                                            | Y                                                                                          | Y                                                                                        | Y                                                                                      | Y                                                                                          | Y                                                                                            | N                                                                                          |
| [numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification) | N                                                                                      | Y                                                                                                               | Y                                                                                          | N                                                                                      | N                                                                                      | Y                                                                                                | N                                                                                                         | Y                                                                                                         | Y                                                                                                       | Y                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | Y                                                                                            | N                                                                                          |
| [STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)                      | N                                                                                      | Y                                                                                                               | Y                                                                                          | Y                                                                                      | Y                                                                                      | Y                                                                                                | Y                                                                                                         | Y                                                                                                         | Y                                                                                                       | Y                                                                                            | Y                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | Y                                                                                            | N                                                                                          |
| [DATE](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md)                          | N                                                                                      | N                                                                                                               | Y                                                                                          | Y                                                                                      | N                                                                                      | Y                                                                                                | Y                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | Y                                                                                            | N                                                                                          |
| [TIME](https://docs.databricks.com/aws/en/sql/language-manual/data-types/time-type.md)                          | N                                                                                      | N                                                                                                               | Y                                                                                          | N                                                                                      | Y                                                                                      | N                                                                                                | N                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | N                                                                                            | N                                                                                          |
| [TIMESTAMP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md)                | N                                                                                      | Y                                                                                                               | Y                                                                                          | Y                                                                                      | N                                                                                      | Y                                                                                                | Y                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | Y                                                                                            | N                                                                                          |
| [TIMESTAMP\_NTZ](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md)       | N                                                                                      | N                                                                                                               | Y                                                                                          | Y                                                                                      | N                                                                                      | Y                                                                                                | Y                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | Y                                                                                            | N                                                                                          |
| [year-month interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md)       | N                                                                                      | Y                                                                                                               | Y                                                                                          | N                                                                                      | N                                                                                      | N                                                                                                | N                                                                                                         | Y                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | N                                                                                            | N                                                                                          |
| [day-time interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md)         | N                                                                                      | Y                                                                                                               | Y                                                                                          | N                                                                                      | N                                                                                      | N                                                                                                | N                                                                                                         | N                                                                                                         | Y                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | N                                                                                            | N                                                                                          |
| [BOOLEAN](https://docs.databricks.com/aws/en/sql/language-manual/data-types/boolean-type.md)                    | N                                                                                      | Y                                                                                                               | Y                                                                                          | N                                                                                      | N                                                                                      | Y                                                                                                | N                                                                                                         | N                                                                                                         | N                                                                                                       | Y                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | Y                                                                                            | N                                                                                          |
| [BINARY](https://docs.databricks.com/aws/en/sql/language-manual/data-types/binary-type.md)                      | N                                                                                      | Y                                                                                                               | Y                                                                                          | N                                                                                      | N                                                                                      | N                                                                                                | N                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | Y                                                                                          | N                                                                                        | N                                                                                      | N                                                                                          | Y                                                                                            | N                                                                                          |
| [ARRAY](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md)                        | N                                                                                      | N                                                                                                               | Y                                                                                          | N                                                                                      | N                                                                                      | N                                                                                                | N                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | Y                                                                                        | N                                                                                      | N                                                                                          | Y                                                                                            | N                                                                                          |
| [MAP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md)                          | N                                                                                      | N                                                                                                               | Y                                                                                          | N                                                                                      | N                                                                                      | N                                                                                                | N                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | Y                                                                                      | N                                                                                          | N                                                                                            | N                                                                                          |
| [STRUCT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/struct-type.md)                      | N                                                                                      | N                                                                                                               | Y                                                                                          | N                                                                                      | N                                                                                      | N                                                                                                | N                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | N                                                                                      | Y                                                                                          | N                                                                                            | N                                                                                          |
| [VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)                    | N                                                                                      | Y                                                                                                               | Y                                                                                          | Y                                                                                      | N                                                                                      | Y                                                                                                | Y                                                                                                         | N                                                                                                         | N                                                                                                       | Y                                                                                            | Y                                                                                          | Y                                                                                        | Y                                                                                      | Y                                                                                          | Y                                                                                            | N                                                                                          |
| [OBJECT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/object-type.md)                      | N                                                                                      | N                                                                                                               | N                                                                                          | N                                                                                      | N                                                                                      | N                                                                                                | N                                                                                                         | N                                                                                                         | N                                                                                                       | N                                                                                            | N                                                                                          | N                                                                                        | Y                                                                                      | Y                                                                                          | N                                                                                            | N                                                                                          |

DECFLOAT is an exception to the numeric entries in this table. See [DECFLOAT](https://docs.databricks.com/aws/en/sql/language-manual/functions/cast.md#decfloat).

## Common error conditions

* [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md)
* [CAST\_OVERFLOW](https://docs.databricks.com/aws/en/error-messages/error-classes.md#cast_overflow)
* [DATATYPE\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/datatype-mismatch-error-class.md)
* [NUMERIC\_VALUE\_OUT\_OF\_RANGE](https://docs.databricks.com/aws/en/error-messages/numeric-value-out-of-range-error-class.md)
* [UNSUPPORTED\_DATATYPE](https://docs.databricks.com/aws/en/error-messages/error-classes.md#unsupported_datatype)

## Rules and limitations based on targetType

> [!WARNING]
>
> In Databricks Runtime, if [spark.sql.ansi.enabled](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-ansi-compliance.md) is `false`, an overflow will not cause an error but instead will “wrap” the result.
>
> A `sourceExpr` value with an invalid format or invalid characters for `targetType` will result in a `NULL`.

### numeric

For casts involving [DECFLOAT](https://docs.databricks.com/aws/en/sql/language-manual/functions/cast.md#decfloat), use the DECFLOAT rules below.

If the `targetType` is a [numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` of the specified numeric type.

* **[numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification)**

  If `targetType` is an [integral numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification), the result is `sourceExpr` **truncated** to a whole number.

  Otherwise, the result is `sourceExpr` **rounded** to a fit the available scale of `targetType`.

  If the value is outside the range of `targetType`, Databricks raises [CAST\_OVERFLOW](https://docs.databricks.com/aws/en/error-messages/error-classes.md#cast_overflow).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn overflow errors into `NULL`.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  `sourceExpr` is read as a literal value of the `targetType`.

  If `sourceExpr` doesn't comply with the format for literal values, Databricks raises [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md).

  If the value is outside the range of the `targetType`, Databricks raises [CAST\_OVERFLOW](https://docs.databricks.com/aws/en/error-messages/error-classes.md#cast_overflow).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn overflow and invalid format errors into `NULL`.

* **[TIMESTAMP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md)**

  The result is the number of seconds elapsed between `1970-01-01 00:00:00 UTC` and `sourceExpr`.

  If `targetType` is an [integral numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification), the result is **truncated** to a whole number.

  Otherwise, the result is **rounded** to a fit the available scale of `targetType`.

  If the result is outside the range of `targetType`, Databricks raises [CAST\_OVERFLOW](https://docs.databricks.com/aws/en/error-messages/error-classes.md#cast_overflow).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn overflow errors into `NULL`.

* **[INTERVAL](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md)**

  **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

  Target type must be an [exact numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification).

  Given an `INTERVAL upper_unit TO lower_unit` the result is measured in total number of `lower_unit`.
  If the `lower_unit` is `SECOND`, fractional seconds are stored to the right of the decimal point.
  For all other intervals the result is always an integral number.

* **[BOOLEAN](https://docs.databricks.com/aws/en/sql/language-manual/data-types/boolean-type.md)**

  If `sourceExpr` is:

  * `true`: The result is 1.
  * `false`: The result is 0.
  * `NULL`: The result is `NULL`.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The rules of the type of the actual value of the `VARIANT` type apply.

#### Examples

```sql
> SELECT cast(NULL AS INT);
  NULL

> SELECT cast(5.6 AS INT);
  5

> SELECT cast(5.6 AS DECIMAL(2, 0));
  6

> SELECT cast(-5.6 AS INT);
  -5

> SELECT cast(-5.6 AS DECIMAL(2, 0));
  -6

> SELECT cast(128 AS TINYINT);
  Error: CAST_OVERFLOW

> SELECT cast(128 AS DECIMAL(2, 0));
  Error: CAST_OVERFLOW

> SELECT cast('123' AS INT);
  123

> SELECT cast('123.0' AS INT);
  Error: CAST_INVALID_INPUT

> SELECT cast(TIMESTAMP'1970-01-01 00:00:01' AS LONG);
  1

> SELECT cast(TIMESTAMP'1970-01-01 00:00:00.000001' AS DOUBLE);
  1.0E-6

> SELECT cast(TIMESTAMP'2022-02-01 00:00:00' AS SMALLINT);
  Error: CAST_OVERFLOW
> SELECT cast(true AS BOOLEAN);
  1

> SELECT cast(INTERVAL '1-2' YEAR TO MONTH AS INTEGER);
  14

> SELECT cast(INTERVAL '1:30.5' MINUTE TO SECOND AS DECIMAL(5, 2));
  90.50

> SELECT cast(TRUE AS INT);
  1

> SELECT cast(FALSE AS INT);
  0

> SELECT cast('15'::VARIANT AS INT);
  15
```

### DECFLOAT

[DECFLOAT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/decfloat-type.md) supports casts to and from `STRING` and other numeric types, including between `DECFLOAT(16)` and `DECFLOAT(34)`. Negative-scale `DECIMAL` casts are unsupported. Casts between DECFLOAT and `BOOLEAN`, `BINARY`, `TIMESTAMP`, `INTERVAL`, or `VARIANT` are unsupported.

* Casts to DECFLOAT use `DECFLOAT_ROUNDING_MODE`. With the default `ROUND_HALF_EVEN`, overflow produces signed infinity, including with `TRY_CAST`. Reducing precision can round without an error or `NULL`.
* Casts from DECFLOAT to integral types truncate toward zero. Casts to `DECIMAL` round half up, independently of `DECFLOAT_ROUNDING_MODE`.
* In ANSI mode, casting DECFLOAT to an integral type or `DECIMAL` raises an overflow error for finite values outside the target range, or an invalid-input error for infinity and NaN. With ANSI mode disabled or with `TRY_CAST`, these cases return `NULL`.
* Casts from DECFLOAT to `FLOAT` or `DOUBLE` round to the nearest binary value with ties to even. Overflow can produce signed infinity.

### STRING

If the `sourceExpr` is a `STRING` the resulting `STRING` inherits the collation of `sourceExpr`.
In all other cases the collation of the resulting `STRING` is the [default collation](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-collation.md#default-collation).

To change the collation add the [collate](https://docs.databricks.com/aws/en/sql/language-manual/functions/collate.md) expression.

If the `targetType` is a [`STRING` type](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` string.

* **[exact numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification)**

  The result is the literal number with an optional minus-sign and no leading zeros except for the single digit to the left of the decimal point.
  If the `targetType` is `DECIMAL(p, s)` with `s` greater 0, a decimal point is added and trailing zeros are added up to scale.

* **[floating-point binary](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification)**

  If the absolute number is less that `10,000,000` and greater or equal than `0.001`, the result is expressed without scientific notation with at least one digit on either side of the decimal point.

  Otherwise, Databricks uses a mantissa followed by `E` and an exponent.
  The mantissa has an optional leading minus sign followed by one digit to the left of the decimal point, and the minimal number of digits greater than zero to the right.
  The exponent has and optional leading minus sign.

* **[DECFLOAT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/decfloat-type.md)**

  Finite values use a signed integer coefficient followed by `E` and a signed exponent. Trailing zeros are preserved, so equal values can produce different strings: `1.5` becomes `+15E-1`, while `1.50` becomes `+150E-2`.

* **[DATE](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md)**

  If the year is between 9999 BCE and 9999 CE, the result is a [dateString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md#literals) of the form `-YYYY-MM-DD` and `YYYY-MM-DD` respectively.

  For years prior or after this range, the necessary number of digits are added to the year component and `+` is used for CE.

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

  The result is a [timeString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/time-type.md#literals) of the form `hh:mm:ss`. Fractional seconds `.f...` are added if necessary.

* **[TIMESTAMP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md)**

  If the year is between 9999 BCE and 9999 CE, the result is a [timestampString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md#literals) of the form `-YYYY-MM-DD hh:mm:ss` and `YYYY-MM-DD hh:mm:ss` respectively.

  For years prior or after this range, the necessary number of digits are added to the year component and `+` is used for CE.

  Fractional seconds `.f...` are added if necessary. For `TIMESTAMP(p)`, the result contains at most `p` fractional digits and trailing zeros are omitted.

* **[TIMESTAMP\_NTZ](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md)**

  If the year is between 9999 BCE and 9999 CE, the result is a [timestampString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md#literals) of the form `-YYYY-MM-DD hh:mm:ss` and `YYYY-MM-DD hh:mm:ss` respectively.

  For years prior or after this range, the necessary number of digits are added to the year component and `+` is used for CE.

  Fractional seconds `.f...` are added if necessary. For `TIMESTAMP_NTZ(p)`, the result contains at most `p` fractional digits and trailing zeros are omitted.

* **[year-month interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md)**

  The result is its shortest representation of the [interval literal](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md#literals).
  If the interval is negative, the sign is embedded in the `interval-string`. For units smaller than 10,leading zeros are omitted.

  A typical year-month interval string has the form:

  * `INTERVAL 'Y' YEAR`
  * `INTERVAL 'Y-M' YEAR TO MONTH`
  * `INTERVAL 'M' MONTH`

* **[day-time interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md)**

  The result is its shortest representation of the [interval literal](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md#literals).
  If the interval is negative, the sign is embedded in the `interval-string`. For units smaller than 10, leading zeros are omitted.

  A typical day time interval string has the form:

  * `INTERVAL 'D' DAY`
  * `INTERVAL 'D h' DAY TO HOUR`
  * `INTERVAL 'D h:m' DAY TO MINUTE`
  * `INTERVAL 'D h:m:s' DAY TO SECOND`
  * `INTERVAL 'h' HOUR`
  * `INTERVAL 'h:m' HOUR TO MINUTE`
  * `INTERVAL 'm:s' MINUTE TO SECOND`
  * `INTERVAL 's' SECOND`

* **[BOOLEAN](https://docs.databricks.com/aws/en/sql/language-manual/data-types/boolean-type.md)**

  The result of the `true` boolean is the `STRING` literal `true`. For `false` it's the STRING literal `false`. For `NULL` it's the NULL string.

* **[BINARY](https://docs.databricks.com/aws/en/sql/language-manual/data-types/binary-type.md)**

  A result is the binary `sourceExpr` interpreted as a UTF-8 character sequence.

  Databricks doesn't validate the UTF-8 characters. A cast from `BINARY` to `STRING` will never inject substitution characters or raise an error.

* **[ARRAY](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md)**

  The result is a comma separated list of cast elements, which is braced with square brackets `[ ]`. One space follows each comma.
  A `NULL` element is translated to a literal `null`.

  Databricks doesn't quote or otherwise mark individual elements, which may themselves contain brackets or commas.

* **[MAP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md)**

  The result is a comma separated list of cast key value pairs, which is braced with curly braces `{ }`. One space follows each comma.
  Each key value pair is separated by a `->`.
  A `NULL` map value is translated to literal `null`.

  Databricks doesn't quote or otherwise mark individual keys or values, which may themselves may contain curly braces, commas or `->`.

* **[STRUCT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/struct-type.md)**

  The result is a comma separated list of cast field values, which is braced with curly braces `{ }`. One space follows each comma.
  A `NULL` field value is translated to a literal `null`.

  Databricks doesn't quote or otherwise mark individual field values, which may themselves may contain curly braces, or commas.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The rules of the type of the actual value of the `VARIANT` type apply.

#### Examples

```sql
> SELECT cast(NULL AS STRING);
  NULL

> SELECT cast(-3Y AS STRING);
  -3

> SELECT cast(5::DECIMAL(10, 5) AS STRING);
  5.00000

> SELECT cast(12345678e-4 AS STRING);
  1234.5678

> SELECT cast(1e7 as string);
  1.0E7

> SELECT cast(1e6 as string);
  1000000.0

> SELECT cast(1e-4 as string);
  1.0E-4

> SELECT cast(1e-3 as string);
  0.001

> SELECT cast(12345678e7 AS STRING);
  1.2345678E14

> SELECT cast(DATE'1900-12-31' AS STRING);
  1900-12-31

-- Caesar no more
> SELECT cast(DATE'-0044-03-15' AS STRING);
  -0044-03-15

> SELECT cast(DATE'100000-12-31' AS STRING);
  +100000-12-31

> SELECT cast(current_timestamp() AS STRING);
  2022-04-02 22:29:09.783

> SELECT cast(TIMESTAMP_NTZ'2023-01-01' AS STRING);
  2023-01-01 00:00:00

> SELECT cast(INTERVAL -'13-02' YEAR TO MONTH AS STRING);
  INTERVAL '-13-2' YEAR TO MONTH

> SELECT cast(INTERVAL '12:04.9900' MINUTE TO SECOND AS STRING);
  INTERVAL '12:04.99' MINUTE TO SECOND

> SELECT cast(true AS STRING);
  true

> SELECT cast(false AS STRING);
  false

-- A bad UTF-8 string
> SELECT cast(x'33800033' AS STRING);
  3�3

> SELECT hex(cast(x'33800033' AS STRING));
  33800033

> SELECT cast(array('hello', NULL, 'world') AS STRING);
  [hello, null, world]

> SELECT cast(array('hello', 'wor, ld') AS STRING);
  [hello, wor, ld]

> SELECT cast(array() AS STRING);
  []

> SELECT cast(map('hello', 1, 'world', null) AS STRING);
  {hello -> 1, world -> null}

> SELECT cast(map('hello -> 1', DATE'2022-01-01') AS STRING);
  {hello -> 1 -> 2022-01-01}

> SELECT cast(map() AS STRING);
  {}

> SELECT cast(named_struct('a', 5, 'b', 6, 'c', NULL) AS STRING);
  {5, 6, null}

> SELECT cast(named_struct() AS STRING);
  {}

> SELECT cast(DATE'2024-01-05'::VARIANT AS STRING);
  2024-01-05

> SELECT cast(5 AS STRING) COLLATE UNICODE;
  5
```

### DATE

If the `targetType` is a [`DATE` type](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` `DATE`.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  `sourceExpr` must be a valid [dateString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md#literals).

  If `sourceExpr` is not a valid `dateString`, Databricks raises [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data errors into `NULL`.

* **[TIMESTAMP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md)**

  The result is date portion of the timestamp `sourceExpr`.

* **[TIMESTAMP\_NTZ](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md)**

  The result is date portion of the timestamp\_ntz `sourceExpr`.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The type rules for the actual value held by the `VARIANT` data type apply.

#### Examples

```sql
> SELECT cast(NULL AS DATE);
  NULL

> SELECT cast('1900-10-01' AS DATE);
  1900-10-01

> SELECT cast('1900-10-01' AS DATE);
  1900-10-01

-- There is no February 30.
> SELECT cast('1900-02-30' AS DATE);
  Error: CAST_INVALID_INPUT

> SELECT cast(TIMESTAMP'1900-10-01 12:13:14' AS DATE);
  1900-10-01

> SELECT cast(TIMESTAMP_NTZ'1900-10-01 12:13:14' AS DATE);
  1900-10-01

> SELECT cast(TIMESTAMP_NTZ'1900-10-01 12:13:14'::VARIANT AS DATE);
  1900-10-01
```

### TIMESTAMP

When `targetType` specifies a precision, fractional-second digits beyond the target precision are truncated. Widening to a greater precision preserves the value.

If the `targetType` is a [`TIMESTAMP` type](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` `DATE`.

* **[numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification)**

  `sourceExpr` is read as the number of seconds since `1970-01-01 00:00:00 UTC`.

  Fractions smaller than microseconds are truncated.

  If the value is outside of the range of `TIMESTAMP`, Databricks raises [CAST\_OVERFLOW](https://docs.databricks.com/aws/en/error-messages/error-classes.md#cast_overflow).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn overflow errors into `NULL`.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  `sourceExpr` must be a valid [timestampString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md#literals).

  If `sourceExpr` has more fractional digits than the target precision, the extra digits are truncated.

  If `sourceExpr` is not a valid `timestampString`, Databricks raises [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data errors into `NULL`.

* **[DATE](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md)**

  The result is the `sourceExpr` `DATE` at `00:00:00`hrs.

* **[TIMESTAMP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md)**

  If the target precision is greater than or equal to the source precision, the value is unchanged. If the target precision is lower, fractional-second digits beyond the target precision are truncated.

* **[TIMESTAMP\_NTZ](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md)**

  The result has the same year, month, day, hour, minute, and second fields as `sourceExpr`. Fractional-second digits beyond the target precision are truncated.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The type rules for the actual value held by the `VARIANT` data type apply.

#### Examples

```sql
> SELECT cast(NULL AS TIMESTAMP);
  NULL

> SET TIME ZONE '+00:00';
> SELECT cast(0.0 AS TIMESTAMP);
  1970-01-01 00:00:00

> SELECT cast(0.0000009 AS TIMESTAMP);
  1970-01-01 00:00:00

> SELECT cast(1e20 AS TIMESTAMP);
  Error: CAST_OVERFLOW

> SELECT cast('1900' AS TIMESTAMP);
  1900-01-01 00:00:00

> SELECT cast('1900-10-01 12:13:14' AS TIMESTAMP);
  1900-10-01 12:13:14

> SELECT cast('1900-02-30 12:13:14' AS TIMESTAMP);
  Error: CAST_INVALID_INPUT

> SELECT cast(DATE'1900-10-01' AS TIMESTAMP);
  1900-10-01 00:00:00

> SELECT cast(TIMESTAMP_NTZ'2023-01-01 02:03:04.567' as TIMESTAMP)
  2023-01-01 02:03:04.567

> SELECT cast(TIMESTAMP'2026-08-18 14:30:45.123456789' AS TIMESTAMP(6));
  2026-08-18 14:30:45.123456

> SELECT cast(DATE'1900-10-01'::VARIANT AS TIMESTAMP);
  1900-10-01 00:00:00
```

### TIMESTAMP\_NTZ

When `targetType` specifies a precision, fractional-second digits beyond the target precision are truncated. Widening to a greater precision preserves the value.

If the `targetType` is a [`TIMESTAMP_NTZ` type](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` `DATE`.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  `sourceExpr` must be a valid [timestampString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md#literals).

  If `sourceExpr` has more fractional digits than the target precision, the extra digits are truncated.

  If `sourceExpr` is not a valid `timestampString`, Databricks raises [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data errors into `NULL`.

* **[DATE](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md)**

  The result is the `sourceExpr` DATE at `00:00:00`hrs.

* **[TIMESTAMP\_NTZ](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md)**

  If the target precision is greater than or equal to the source precision, the value is unchanged. If the target precision is lower, fractional-second digits beyond the target precision are truncated.

* **[TIMESTAMP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md)**

  The result is local time as the `sourceExpr` in the session time zone. Fractional-second digits beyond the target precision are truncated.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The type rules for the actual value held by the `VARIANT` data type apply.

#### Examples

```sql
> SELECT cast(NULL AS TIMESTAMP_NTZ);
  NULL

> SELECT cast('1900' AS TIMESTAMP_NTZ);
  1900-01-01 00:00:00

> SELECT cast('1900-10-01 12:13:14' AS TIMESTAMP_NTZ);
  1900-10-01 12:13:14

> SELECT cast('1900-02-30 12:13:14' AS TIMESTAMP_NTZ);
  Error: CAST_INVALID_INPUT

> SELECT cast(DATE'1900-10-01' AS TIMESTAMP_NTZ);
  1900-10-01 00:00:00

> SELECT current_timezone(), CAST(TIMESTAMP'2021-7-1T8:43:28' as TIMESTAMP_NTZ);
  America/Los_Angeles 2021-07-01 08:43:28

> SELECT current_timezone(), CAST(TIMESTAMP'2021-7-1T8:43:28UTC+3' as TIMESTAMP_NTZ);
  America/Los_Angeles 2021-06-30 22:43:28

> SELECT cast(DATE'1900-10-01'::VARIANT AS TIMESTAMP_NTZ);
  1900-10-01 00:00:00
```

### TIME

If the `targetType` is a [`TIME` type](https://docs.databricks.com/aws/en/sql/language-manual/data-types/time-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` `TIME`.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  `sourceExpr` must be a valid [timeString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/time-type.md#literals).

  If `sourceExpr` is not a valid `timeString`, Databricks raises [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data errors into `NULL`.

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

  If the target precision is lower, the fractional seconds are **truncated** (not rounded). If the target precision is higher, the result is padded with trailing zeros.

#### Examples

```sql
> SELECT CAST(NULL AS TIME);
  NULL

> SELECT CAST('10:30:00' AS TIME);
  10:30:00

> SELECT CAST('25:00:00' AS TIME);
  Error: CAST_INVALID_INPUT

> SELECT CAST(TIME'09:15:30.123456' AS TIME(3));
  09:15:30.123
```

### year-month interval

If the `targetType` is a [year-month interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` year-month interval.

* **[integral\_numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification)**

  **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

  The numeric is interpreted as number of lower units of the `targetType` [yearmonthIntervalQualifier](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md).

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  `sourceExpr` must be a valid [yearMonthIntervalString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md#literals).

  If `sourceExpr` is not a valid `yearMonthIntervalString`, Databricks raises [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data errors into `NULL`.

* **[year-month interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md)**

  If the `targetType` [yearMonthIntervalQualifier](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md) includes `MONTH` the value remains unchanged, but is reinterpreted to match the target type.

  Otherwise, if the source type [yearMonthIntervalQualifier](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md) includes `MONTH`, the result is truncated to full years.

#### Examples

```sql
> SELECT cast(NULL AS INTERVAL YEAR);
  NULL

> SELECT cast('1-4' AS INTERVAL YEAR TO MONTH)::STRING;
  INTERVAL '1-4' YEAR TO MONTH

> SELECT cast('1' AS INTERVAL YEAR TO MONTH);
  Error: CAST_INVALID_INPUT

> SELECT cast(INTERVAL '1-4' YEAR TO MONTH AS INTERVAL MONTH)::STRING;
  INTERVAL '16' MONTH

> SELECT cast(14 AS INTERVAL YEAR TO MONTH)::STRING;
  INTERVAL '1-2' YEAR TO MONTH

> SELECT cast(INTERVAL '1-11' YEAR TO MONTH AS INTERVAL YEAR)::STRING;
  INTERVAL '1' YEAR
```

### day-time interval

If the `targetType` is a [day-time interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` day-time interval.

* **[exact\_numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification)**

  **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

  The numeric is interpreted as number of lower units of the `targetType` [dayTimeIntervalQualifier](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md).
  If the unit is `SECOND` any fractions are interpreted as fractional seconds.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  `sourceExpr` must be a valid [dayTimeIntervalString](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md#literals).

  If `sourceExpr` is not a valid `dayTimeIntervalString`, Databricks raises [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data errors into `NULL`.

* **[day-time interval](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md)**

  If the `targetType` [dayTimeIntervalQualifier](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md) includes the smallest unit of the source type [dayTimeIntervalQualifier](https://docs.databricks.com/aws/en/sql/language-manual/data-types/interval-type.md), the value remains unchanged, but is reinterpreted to match the target type.

  Otherwise, the `sourceExpr` interval is truncated to fit the `targetType`.

```sql
> SELECT cast(NULL AS INTERVAL HOUR);
  NULL

> SELECT cast('1 4:23' AS INTERVAL DAY TO MINUTE)::STRING;
  INTERVAL '1 04:23' DAY TO MINUTE

> SELECT cast('1' AS INTERVAL DAY TO MINUTE);
  Error: CAST_INVALID_INPUT

> SELECT cast(INTERVAL '1 4:23' DAY TO MINUTE AS INTERVAL MINUTE)::STRING;
  INTERVAL '1703' MINUTE

> SELECT cast(INTERVAL '1 4:23' DAY TO MINUTE AS INTERVAL HOUR)::STRING;
  INTERVAL '28' HOUR

> SELECT cast(125.3 AS INTERVAL MINUTE TO SECOND)::STRING;
  INTERVAL '2:5.3' MINUTE TO SECOND
```

### BOOLEAN

If the `targetType` is a [BOOLEAN](https://docs.databricks.com/aws/en/sql/language-manual/data-types/boolean-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` of type `BOOLEAN`.

* **[numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification)**

  If `sourceExpr` is:

  * `0`: The result is `false`.

    * `NULL`: The result is `NULL`.
    * [`special floating point value`](https://docs.databricks.com/aws/en/sql/language-manual/data-types/special-floating-point-values.md): The result is `true`.

    Otherwise, the result is `true`.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  If `sourcEexpr` is (case insensitive):

  * `'T', 'TRUE', 'Y', 'YES', or '1'`: The result is `true`
  * `'F', 'FALSE', 'N', 'NO', or '0'`: The result is `false`
  * `NULL`: The result is `NULL`

  Otherwise, Databricks raises [CAST\_INVALID\_INPUT](https://docs.databricks.com/aws/en/error-messages/cast-invalid-input-error-class.md).

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data errors into `NULL`.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The rules of the type of the actual value of the `VARIANT` type apply.

#### Examples

```sql
> SELECT cast(NULL AS BOOLEAN);
  NULL

> SELECT cast('T' AS BOOLEAN);
  true

> SELECT cast('True' AS BOOLEAN);
  true

> SELECT cast('1' AS BOOLEAN);
  true

> SELECT cast('0' AS BOOLEAN);
  false

> SELECT cast('n' AS BOOLEAN);
  false

> SELECT cast('on' AS BOOLEAN);
  Error: CAST_INVALID_INPUT

> SELECT cast(0 AS BOOLEAN);
  false

> SELECT cast(0.0E10 AS BOOLEAN);
  false

> SELECT cast(1 AS BOOLEAN);
  true

> SELECT cast(0.1 AS BOOLEAN);
  true

> SELECT cast('NaN'::FLOAT AS BOOLEAN);
  true

> SELECT cast(1::VARIANT AS BOOLEAN);
  true
```

### BINARY

If the `targetType` is a [BINARY](https://docs.databricks.com/aws/en/sql/language-manual/data-types/binary-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` of type `BINARY`.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  The result is the UTF-8 encoding of the `surceExpr`.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The type rules for the actual value held by the `VARIANT` data type apply.

#### Examples

```sql
> SELECT cast(NULL AS BINARY);
  NULL

> SELECT hex(cast('Spark SQL' AS BINARY));
  537061726B2053514C

> SELECT hex(cast('Oдesa' AS BINARY));
  4FD0B4657361

> SELECT hex(cast('Oдesa'::VARIANT AS BINARY));
  4FD0B4657361
```

### ARRAY

If the `targetType` is an [ARRAY \< targetElementType >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` of the `targeType`.

* **[ARRAY \< sourceElementType >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md)**

  If the cast from `sourceElementType` to `targetElementType` is supported, the result is an `ARRAY<targetElementType>` with all elements cast to the `targetElementType`.

  Databricks raises an error if the cast isn't supported or if any of the elements can't be cast.

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data or overflow errors into `NULL`.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The type rules for the actual value held by the `VARIANT` data type apply.

#### Examples

```sql
> SELECT cast(NULL AS ARRAY<INT>);
  NULL

> SELECT cast(array('t', 'f', NULL) AS ARRAY<BOOLEAN>);
  [true, false, NULL]

> SELECT cast(array('t', 'f', NULL) AS INTERVAL YEAR);
  Error: DATATYPE_MISMATCH

> SELECT cast(array('t', 'f', 'o') AS ARRAY<BOOLEAN>);
  Error: CAST_INVALID_INPUT

> SELECT cast(array('t', 'f', NULL)::VARIANT AS ARRAY<BOOLEAN>);
  [true, false, NULL]
```

### MAP

If the `targetType` is an [MAP \< targetKeyType, targetValueType >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/map-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` of the `targetType`.

* **[MAP \<sourceKeyType, sourceValueType >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/map-type.md)**

  If the casts from `sourceKeyType` to `targetKeyType` and `sourceValueType` to `targetValueType` are supported,
  the result is an `MAP<targetKeyType, targetValueType>` with all keys cast to the `targetKeyType` and all values cast to the `targetValueType`.

  Databricks raises an error if the cast isn't supported or if any of the keys or values can't be cast.

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data or overflow errors into `NULL`.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The type rules for the actual value held by the `VARIANT` data type apply.

* **[OBJECT \< \[sourceFieldName : sourceFieldType \[, …\]\] >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/object-type.md)**

  Each `sourceFieldName` of type `STRING` is cast to `targetKeyType` and mapped to a map key.
  Each source field value of `sourceFieldType` is cast `targetValueType` and mapped the respective map value.

  Databricks raises an error if any casts are not supported or if any of the field values or key values can't be cast.

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data or overflow errors into `NULL`.

#### Examples

```sql
> SELECT cast(NULL AS MAP<STRING, INT>);
  NULL

> SELECT cast(map('10', 't', '15', 'f', '20', NULL) AS MAP<INT, BOOLEAN>);
  {10 -> true, 15 -> false, 20 -> null}

> SELECT cast(map('10', 't', '15', 'f', '20', NULL) AS MAP<INT, ARRAY<INT>>);
  Error: DATATYPE_MISMATCH

> SELECT cast(map('10', 't', '15', 'f', '20', 'o') AS MAP<INT, BOOLEAN>);
  Error: CAST_INVALID_INPUT

-- Casting an OBJECT to a MAP
> SELECT schema_of_variant(parse_json('{"cars": 12, "bicycles": 5 }'));
  OBJECT<bicycles: BIGINT, cars: BIGINT>
> SELECT CAST(parse_json('{"cars": 12, "bicycles": 5 }') AS MAP<STRING, INTEGER>);
  {bicycles -> 5, cars -> 12}
```

### STRUCT

If the `targetType` is a [STRUCT \<\[targetFieldName : targetFieldType \[NOT NULL\] \[COMMENT str\] \[, …\]\] >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/struct-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` of the `targetType`.

* **[STRUCT \< \[sourceFieldName : sourceFieldType \[NOT NULL\] \[COMMENT str\] \[, …\]\] >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/struct-type.md)**

  The `sourceExpr` can be cast to `targetType` if all of these conditions are true:

  * The source type has the same number of fields as the target
  * For all fields: `sourceFieldTypeN` can be cast to the `targetFieldTypeN`.
  * For all field values: The source field value N can be cast to `targetFieldTypeN` and the value isn't null if target field N is marked as `NOT NULL`.

  `sourceFieldName`s, source `NOT NULL` constraints, and source `COMMENT`s need not match the `targetType` and are ignored.

  Databricks raises an error if the cast isn't supported or if any of the fields can't be cast.

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data or overflow errors into `NULL`.

* **[VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md)**

  The type rules for the actual value held by the `VARIANT` data type apply.

* **[OBJECT \< \[sourceFieldName : sourceFieldType \[, …\]\] >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/object-type.md)**

  All `sourceFieldName`s are matched to `sourceFieldName`s.
  Each source field value of `sourceFieldType` is cast to the matched `targetValueType` and mapped to the respective map value.

  If a `targetFieldName` is not matched, the field value is `NULL`.

  If a `sourceFieldName` is not matched, the field is ignored.

  Databricks raises an error if any casts are not supported or if any of the field values or key values can't be cast.

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data or overflow errors into `NULL`.

#### Examples

```sql
> SELECT cast(NULL AS STRUCT<a:INT>);
  NULL

> SELECT cast(named_struct('a', 't', 'b', '1900-01-01') AS STRUCT<b:BOOLEAN, c:DATE NOT NULL COMMENT 'Hello'>);
  {"b":true,"c":1900-01-01}

> SELECT cast(named_struct('a', 't', 'b', NULL::DATE) AS STRUCT<b:BOOLEAN, c:DATE NOT NULL COMMENT 'Hello'>);
  Error: DATATYPE_MISMATCH

> SELECT cast(named_struct('a', 't', 'b', '1900') AS STRUCT<b:BOOLEAN, c:ARRAY<INT>>);
  Error: DATATYPE_MISMATCH

> SELECT cast(named_struct('a', 't', 'b', 'hello') AS STRUCT<b:BOOLEAN, c:DATE>);
  Error: CAST_INVALID_INPUT

> SELECT cast(named_struct('a', 't', 'b', '1900-01-01')::VARIANT AS STRUCT<b:BOOLEAN, c:DATE NOT NULL COMMENT 'Hello'>);
  {"b":true,"c":1900-01-01}

-- Casting an OBJECT to a STRUCT
> SELECT schema_of_variant(parse_json('{"name": "jason", "age": 25 }'));
  OBJECT<age: BIGINT, name: STRING>
> SELECT CAST(parse_json('{"name": "jason", "age": 25 }') AS STRUCT<id: BIGINT, name: STRING>);
  {"id":null,"name":"jason"}
```

### VARIANT

If the `targetType` is a [VARIANT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/variant-type.md) and `sourceExpr` is of type:

* **[VOID](https://docs.databricks.com/aws/en/sql/language-manual/data-types/null-type.md)**

  The result is a `NULL` of type `VARIANT`.

* **[numeric](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatypes.md#data-type-classification)**

  The result is a `VARIANT`, representing the numeric value.
  The precision of `DECIMAL` types must be \<= 38.

  All integral numerics are mapped to `BIGINT`.

  All `DECIMAL` values are mapped to their narrowest precision and scale.

* **[STRING](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md)**

  The result is a `VARIANT`, representing the `STRING` value.

* **[DATE](https://docs.databricks.com/aws/en/sql/language-manual/data-types/date-type.md)**

  The result is a `VARIANT`, representing the `DATE` value.

* **[TIMESTAMP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-type.md)**

  The result is a `VARIANT`, representing the `TIMESTAMP` value.

* **[TIMESTAMP\_NTZ](https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type.md)**

  The result is a `VARIANT`, representing the `TIMESTAMP NTZ` value.

* **[BOOLEAN](https://docs.databricks.com/aws/en/sql/language-manual/data-types/boolean-type.md)**

  The result is a `VARIANT`, representing the `BOOLEAN` value.

* **[BINARY](https://docs.databricks.com/aws/en/sql/language-manual/data-types/binary-type.md)**

  The result is a `VARIANT`, representing the `BINARY` value.

* **[ARRAY \< sourceElementType >](https://docs.databricks.com/aws/en/sql/language-manual/data-types/array-type.md)**

  If the cast from `sourceElementType` to `VARIANT` is supported, the result is `VARIANT`, representing `ARRAY<sourceElementType>`.

  Databricks raises an error if the cast isn't supported.

* **[STRUCT](https://docs.databricks.com/aws/en/sql/language-manual/data-types/struct-type.md)**

  This cast is not directly supported since `VARIANT` cannot preserve the order of struct fields. Use [to\_variant\_object](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_variant_object.md) to convert the `STRUCT` to `VARIANT` as an `OBJECT`.

* **[MAP](https://docs.databricks.com/aws/en/sql/language-manual/data-types/map-type.md)**

  This cast is not directly supported since `VARIANT` cannot preserve the `MAP` type.
  Use [to\_variant\_object](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_variant_object.md) to convert the `MAP` to `VARIANT` as an `OBJECT`.
  The map keys must be strings.

  Databricks raises an error if the cast isn't supported.

  Use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) to turn invalid data or overflow errors into `NULL`.

#### Examples

```sql
> SELECT cast(NULL AS VARIANT);
  NULL

> SELECT cast(5.1000 AS VARIANT);
  5.1

> SELECT schema_of_variant(cast(5 AS VARIANT));
  BIGINT
```

## Related functions

* [`::` (colon colon sign) operator](https://docs.databricks.com/aws/en/sql/language-manual/functions/coloncolonsign.md)
* [`?::` (question double colon sign) operator](https://docs.databricks.com/aws/en/sql/language-manual/functions/questiondoublecolonsign.md)
* [`try_cast` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md)
