CAST
All functions > CORE > CAST
Returns the given expression casted to the given type, or raise an exception if the conversion fails.
Signatures
Returns: The expression converted to the specified type
CAST(expr: T, as_type: DataTypeEnum.TYPE) → U sql
| Parameter | Type | Required | Description |
|---|---|---|---|
expr | T | Yes | The expression to convert |
as_type | DataTypeEnum.TYPE | Yes | The target data type |
Notes
- Converts a value from one data type to another
- Supports conversions between numeric types (BIGINT, DECIMAL, DOUBLE)
- Supports conversions between numeric and string types (VARCHAR)
- Special handling for ROW and ARRAY types
- Conversion may fail if the value cannot be represented in the target type
- For safe casting that returns NULL on failure, use TRY_CAST
- NULL values remain NULL after casting
- Precision may be lost when converting between numeric types
- Follows SQL standard casting rules
Examples
FeatureQL
SELECT
f1 := CAST(42 AS DOUBLE), -- Integer to double
f2 := CAST(3.14e0 AS BIGINT), -- Double to integer (truncates)
f3 := CAST('123' AS BIGINT), -- String to integer
f4 := CAST(42 AS VARCHAR), -- Integer to string
f5 := CAST(3.14 AS VARCHAR), -- Decimal to string
f6 := CAST('2024-01-15' AS DATE) -- String to date
;Result
| f1 VARCHAR | f2 BIGINT | f3 BIGINT | f4 VARCHAR | f5 VARCHAR | f6 TIMESTAMP |
|---|---|---|---|---|---|
| 42.0 | 3 | 123 | 42 | 3.14 | 2024-01-15 |
On this page