> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e6data.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CAST

> Converts a value to a requested SQL type.

<div className="e6-meta"><span className="e6-eyebrow e6-eyebrow--quiet">Scalar function</span></div>

A NULL value remains NULL for a supported conversion. Invalid string-to-numeric values currently produce NULL even when ANSI mode is enabled.

## Syntax

### Scalar

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
cast(value AS target_type)
```

## Arguments

| Name | Type | Required | Description |
| - | - | - | - |
| value | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT or VARIANT | Yes | Value to convert. Source and target must form a supported conversion pair; listing a source type does not permit conversion to every target. See the conversion restrictions below. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). |
| target\_type | SQL type specification | Yes | Requested target type. Specify BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME or TIMESTAMP. ARRAY, MAP and STRUCT targets require complete nested type definitions and do not perform recursive element conversion. The source/target pair must be a supported conversion. Must be a constant argument. |

## Returns

* Type: Requested target SQL type
* NULL behavior: A NULL value remains NULL for a supported conversion. Invalid string-to-numeric values currently produce NULL even when ANSI mode is enabled.
* Shape: For plain CAST to ARRAY/MAP/STRUCT, the input is passed through as an already-shaped value; TRY\_CAST does not implement that complex-type annotation shortcut.
* Ordering: Plain CAST does not reorder elements in its complex-type annotation path.

## Properties

| Property | Value |
| - | - |
| Deterministic | Yes |
| Supports window usage | No |
| Exact result | Partial (Conversions can truncate, round or replace invalid encoding; not every conversion preserves the input value exactly.) |

## Usage notes

* Write the target type directly in the call. It cannot be a STRING expression that changes for each row.
* String-to-integer conversion first parses as DOUBLE and then truncates toward zero.
* String-to-DATE and plain STRING-to-TIMESTAMP paths use ANSI-aware parsing: invalid non-NULL values can raise errors in ANSI mode and return NULL otherwise.
* Binary-to-string conversion replaces invalid UTF-8 bytes with U+FFFD.
* Casting a ROW, MAP, or ARRAY changes its declared type without converting the contained values.
* INTEGER and INT mean the same type; STRING means VARCHAR text. NULL can be used with a supported type. Cast NULL explicitly when its type would otherwise be unclear.
* Not every source type can be converted to every target type. MAP-to-STRING and STRUCT-to-STRING conversions are rejected. Casting ARRAY, MAP, or STRUCT does not recursively convert its elements or fields.
* Deterministic: The same inputs and settings produce the same result.

## Examples

### Typical behavior

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT CAST('42' AS INT) AS "value", CAST(CAST(NULL AS STRING) AS INT) AS null_value;
```

| value | null\_value |
| - | - |
| 42 | NULL |

### Convert text to decimal and date

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
WITH sample AS (
  SELECT id, CASE WHEN id = 0 THEN '12.50' ELSE '7.25' END AS price_text,
    CASE WHEN id = 0 THEN '2024-03-15' ELSE '2024-03-16' END AS date_text
  FROM range(0, 2)
)
SELECT CAST(price_text AS DECIMAL(8,2)) AS price, CAST(date_text AS DATE) AS order_date FROM sample ORDER BY id;
```

| price | order\_date |
| - | - |
| 12.50 | 2024-03-15 |
| 7.25 | 2024-03-16 |

## Limitations

* Unsupported conversions or incomplete target-type definitions can cause the query to be rejected before execution. TRY\_CAST does not turn those errors into NULL.
* CHAR and VARCHAR length limits are not enforced by this cast. Do not rely on the cast to truncate text.
* Precision loss and error handling depend on the input type, target type, ANSI mode, and conversion rules described above.
* VARIANT-to-scalar conversions follow VARIANT\_GET’s rules. When converting to a narrower integer type, do not rely on Spark-style wraparound behavior.

## Related functions

* [typeof](/query/sql/functions/conditional/typeof)
* [try\_cast](/query/sql/functions/conditional/try_cast)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.