# CAST and TRY_CAST
Returns the value converted to the specified type

> For the complete documentation index, see [llms.txt](https://preview.materialize.com/materialize/40269/llms.txt).


The `cast` function and operator return a value converted to the specified
[type](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/index.md). The `try_cast` function does the same, but returns `NULL`
instead of an error when the value cannot be converted.

## Signatures



```mzsql
CAST ( <val> AS <type> )

```

| Syntax element | Description |
| --- | --- |
| `<val>` | Any value you want to convert.  |
| **AS** `<type>` | The return value's [type](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/index.md).  |




```mzsql
<val>::<type>

```

| Syntax element | Description |
| --- | --- |
| `<val>` | Any value you want to convert.  |
| `::<type>` | The return value's [type](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/index.md).  |


The following special syntax is permitted if _val_ is a string literal:



```mzsql
<type> '<val>'

```

| Syntax element | Description |
| --- | --- |
| `<type>` | The return value's [type](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/index.md).  |
| `'<val>'` | A string literal value. This special syntax is permitted if `<val>` is a string literal.  |




```mzsql
TRY_CAST ( <val> AS <type> )

```

| Syntax element | Description |
| --- | --- |
| `<val>` | Any value you want to convert.  |
| **AS** `<type>` | The return value's [type](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/index.md).  |


### Return value

`cast` returns the value with the type specified by the _type_ parameter.

`try_cast` returns the value with the type specified by the _type_ parameter,
or `NULL` if the conversion fails. Its result is nullable whenever the
conversion can fail, even when the value being converted is not.

## Details

### Valid casts

Cast context defines when casts may occur.

Cast context | Definition | Strictness
--------|------------|-----------
**Implicit** | Values are automatically converted. For example, when you add `int4` to `int8`, the `int4` value is automatically converted to `int8`. | Least
**Assignment** | Values of one type are converted automatically when inserted into a column of a different type. | Medium
**Explicit** | You must invoke `CAST` deliberately. | Most

Casts allowed in less strict contexts are also allowed in stricter contexts. That is, implicit casts also occur by assignment, and both implicit casts and casts by assignment can be explicitly invoked.

Source type                                | Return type                                   | Cast context
-------------------------------------------|-----------------------------------------------|----------
[`array`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/array/index.md)<sup>1</sup>  | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`bool`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/boolean/index.md)                | Explicit
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Assignment
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Implicit
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)             | Implicit
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Implicit
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)           | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`bool`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/boolean/index.md)             | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Explicit
[`bool`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/boolean/index.md)             | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`bytea`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/bytea/index.md)              | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`date`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/date/index.md)                | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`date`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/date/index.md)                | [`timestamp`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)         | Implicit
[`date`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/date/index.md)                | [`timestamptz`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)       | Implicit
[`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)              | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Assignment
[`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)              | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Assignment
[`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)              | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)<sup>2</sup> | Assignment
[`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)              | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Assignment
[`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)              | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)              | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)              | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)              | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Implicit
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`bool`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/boolean/index.md)                | Explicit
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Implicit
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)             | Implicit
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`oid`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/oid/index.md)                     | Implicit
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Implicit
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`interval`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/interval/index.md)        | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`interval`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/interval/index.md)        | [`time`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/time/index.md)                   | Assignment
[`jsonb`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/jsonb/index.md)              | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Explicit
[`jsonb`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/jsonb/index.md)              | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Explicit
[`jsonb`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/jsonb/index.md)              | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Explicit
[`jsonb`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/jsonb/index.md)              | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Explicit
[`jsonb`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/jsonb/index.md)              | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)             | Explicit
[`jsonb`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/jsonb/index.md)              | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`list`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/list/index.md)<sup>1</sup>    | [`list`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/list/index.md)                   | Implicit
[`list`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/list/index.md)<sup>1</sup>    | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`map`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/map/index.md)                  | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`mz_aclitem`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/mz_aclitem/index.md)    | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Explicit
[`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)          | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Assignment
[`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)          | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Implicit
[`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)          | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Assignment
[`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)          | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Implicit
[`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)          | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)          | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)          | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)          | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`oid`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/oid/index.md)                  | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Assignment
[`oid`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/oid/index.md)                  | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Explicit
[`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Assignment
[`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Implicit
[`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Assignment
[`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)             | Assignment
[`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`record`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/record/index.md)            | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Implicit
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Implicit
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Implicit
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)             | Implicit
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`oid`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/oid/index.md)                     | Implicit
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Implicit
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`smallint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)         | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`bool`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/boolean/index.md)                | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`bytea`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/bytea/index.md)                 | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`date`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/date/index.md)                   | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`interval`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/interval/index.md)           | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`jsonb`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/jsonb/index.md)                 | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`list`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/list/index.md)                   | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`map`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/map/index.md)                     | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)             | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`oid`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/oid/index.md)                     | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`time`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/time/index.md)                   | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`timestamp`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)         | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`timestamptz`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)       | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Explicit
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                | [`uuid`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uuid/index.md)                   | Explicit
[`time`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/time/index.md)                | [`interval`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/interval/index.md)           | Implicit
[`time`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/time/index.md)                | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`timestamp`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)      | [`date`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/date/index.md)                   | Assignment
[`timestamp`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)      | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`timestamp`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)      | [`timestamptz`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)       | Implicit
[`timestamptz`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)    | [`date`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/date/index.md)                   | Assignment
[`timestamptz`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)    | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`timestamptz`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)    | [`timestamp`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/timestamp/index.md)         | Assignment
[`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Implicit
[`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Implicit
[`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Implicit
[`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)             | Implicit
[`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Implicit
[`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Implicit
[`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Implicit
[`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Implicit
[`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Implicit
[`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Assignment
[`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                | [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md)             | Implicit
[`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Implicit
[`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment
[`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                | [`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Implicit
[`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`bigint`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)              | Assignment
[`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md)                 | Implicit
[`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`int`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/integer/index.md)                 | Assignment
[`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`real`](https://preview.materialize.com/materialize/40269/sql/types/real)                   | Implicit
[`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`uint2`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`uint8`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)               | [`uint4`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uint/index.md)                  | Assignment
[`uuid`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/uuid/index.md)                | [`text`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/text/index.md)                   | Assignment

<sup>1</sup> [`Arrays`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/array/index.md) and [`lists`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/list/index.md) are composite types subject to special constraints. See their respective type documentation for details.

<sup>2</sup> Casting a [`float`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/float/index.md) to a [`numeric`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/numeric/index.md) can yield an imprecise result due to the floating point arithmetic involved in the conversion.

### `TRY_CAST`

> **Public Preview:** This feature is in public preview.











`TRY_CAST` accepts the same explicit casts as `CAST`, and returns `NULL` where
`CAST` would return an error: for example, text that does not parse as the
target type, a number that does not fit in the target type, or a JSON value of
the wrong kind. When the conversion succeeds, `TRY_CAST` and `CAST` return the
same value.

A `CAST` that fails on one row makes every query that needs that row's value
fail, for as long as the row is present. In a materialized view or an indexed
view, that is every query against the view, whatever it selects. `TRY_CAST`
returns `NULL` for that row instead.

`TRY_CAST` only suppresses errors from the conversion itself. Errors raised
while computing the value being converted still propagate:

```mzsql
SELECT TRY_CAST(1 / 0 AS text);
```
```nofmt
ERROR:  division by zero
```

A parameter whose type is inferred from `TRY_CAST` is decoded as the target
type when the statement is bound, so a bound value that does not convert is a
bind error rather than `NULL`. To get `NULL` instead, give the parameter an
explicit type:

```mzsql
SELECT TRY_CAST($1::text AS int);
```

Converting a value of a composite type ([`array`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/array/index.md),
[`list`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/list/index.md), or [`record`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/record/index.md)) converts each
element. If any element fails to convert, `TRY_CAST` returns `NULL` for the
whole value rather than a value with `NULL` elements.

`TRY_CAST` accepts every explicit cast that `CAST` does, with these
differences, each of which is reported when the statement is planned:

- `TRY_CAST` does not support the casts that look up objects in the catalog:
  casts to or from `regclass`, `regproc`, `regtype`, `aclitem`, and
  [`mz_aclitem`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/mz_aclitem/index.md), other than those between the `oid`
  alias types and the integer types.

- A non-empty `ARRAY`, `LIST`, or `ROW` constructor written directly inside
  `TRY_CAST` is typed on its own before the conversion, where `CAST` would
  type it from the target. Its elements must therefore already agree on a
  type:

  ```mzsql
  -- With a text column s, CAST(ARRAY[s, 2] AS int[]) plans, but this does not:
  -- "ARRAY types text and integer cannot be matched".
  SELECT TRY_CAST(ARRAY[s, 2] AS int[]) FROM t;
  -- Give the elements a common type instead.
  SELECT TRY_CAST(ARRAY[s, 2::text] AS int[]) FROM t;
  ```

  An empty `ARRAY`, `LIST`, or `MAP` constructor has no elements to convert, so
  it takes its type from the target as it does under `CAST`.

- A non-empty `MAP` constructor written directly inside `TRY_CAST` is
  rejected. It would be typed on its own like the other constructors, and
  there is no cast between two [`map`](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/map/index.md) types to convert it
  afterward. Convert it with `CAST` or `::` instead.

## Examples

```mzsql
SELECT INT '4';
```
```nofmt
 ?column?
----------
         4
```

<hr>

```mzsql
SELECT CAST (CAST (100.21 AS numeric(10, 2)) AS float) AS dec_to_float;
```
```nofmt
 dec_to_float
--------------
       100.21
```

<hr/>

```mzsql
SELECT 100.21::numeric(10, 2)::float AS dec_to_float;
```
```nofmt
 dec_to_float
--------------
       100.21
```

<hr/>

```mzsql
SELECT TRY_CAST('42' AS int) AS ok, TRY_CAST('forty-two' AS int) AS bad;
```
```nofmt
 ok | bad
----+-----
 42 |
```

<hr/>

Routing rows whose values do not convert, instead of failing the whole view:

```mzsql
CREATE VIEW parsed AS
  SELECT raw, TRY_CAST(raw AS timestamp) AS ts
  FROM events;

CREATE VIEW unparseable AS
  SELECT raw FROM parsed WHERE ts IS NULL AND raw IS NOT NULL;
```

## Related topics
* [Data Types](https://preview.materialize.com/materialize/40269/markdown-docs/sql/types/index.md)
