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 AS type ) → U TRY CAST( expr AS type ) → U expr::type → U
| Parameter | Type | Required | Description |
|---|---|---|---|
expr | T | Yes | The expression to convert |
type | TYPE | Yes | Target type, such as BIGINT. _#ENTITY or _@UNIT keeps the datatype and sets the annotation |
Notes
- Converts a value from one data type to another
- Numeric targets include TINYINT, SMALLINT, INT, BIGINT, DECIMAL, FLOAT, and DOUBLE
- Also converts between numerics and 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(expr AS type) - NULL values remain NULL after casting
- Precision may be lost when converting between numeric types
- Halfway values go away from zero on every numeric family (
CAST(2.5 AS BIGINT)is 3,CAST(2.5e0 AS BIGINT)is 3,CAST(-2.5e0 AS BIGINT)is -3). Other magnitudes truncate toward zero.ROUNDon DOUBLE is half away from zero for every value - FeatureQL does not apply SQL implicit widening: write CAST when you change category or family
See also
Examples
With CAST operator
FeatureQL
RETURN
-- Integer to double
f1 := 42::DOUBLE,
-- Double to integer (truncates)
f2 := 3.14e0::BIGINT,
-- String to integer
f3 := '123'::BIGINT,
-- Integer to string
f4 := 42::VARCHAR,
-- Decimal to string
f5 := 3.14::VARCHAR,
-- String to date
f6 := '2024-01-15'::DATE
;Result
| f1 DECIMAL | f2 BIGINT | f3 BIGINT | f4 VARCHAR | f5 VARCHAR | f6 TIMESTAMP |
|---|---|---|---|---|---|
| 42.0 | 3 | 123 | 42 | 3.14 | 2024-01-15 |
INTERVAL to VARCHAR
FeatureQL
RETURN
-- An interval's text is its canonical ISO 8601 form
f1 := INTERVAL '1 year'::VARCHAR,
-- A day is 24 hours, so hours carry into days
f2 := INTERVAL '36 hours'::VARCHAR,
-- Days never carry into months
f3 := INTERVAL '1 month 45 days'::VARCHAR,
-- A negative interval has one leading sign
f4 := (INTERVAL '-2 days')::VARCHAR
;Result
| f1 VARCHAR | f2 VARCHAR | f3 VARCHAR | f4 VARCHAR |
|---|---|---|---|
| P1Y | P1DT12H | P1M45D | -P2D |
Postgres style with :: operator
FeatureQL
RETURN
-- Integer to double
f1 := 42::DOUBLE,
-- Double to integer (truncates)
f2 := 3.14e0::BIGINT,
-- String to integer
f3 := '123'::BIGINT,
-- Integer to string
f4 := 42::VARCHAR,
-- Decimal to string
f5 := 3.14::VARCHAR,
-- String to date
f6 := '2024-01-15'::DATE
;Result
| f1 DECIMAL | f2 BIGINT | f3 BIGINT | f4 VARCHAR | f5 VARCHAR | f6 TIMESTAMP |
|---|---|---|---|---|---|
| 42.0 | 3 | 123 | 42 | 3.14 | 2024-01-15 |
NULL and edge conversions
FeatureQL
RETURN
-- NULL stays NULL with the target type
f1 := NULL::BIGINT,
-- String casts often trim surrounding whitespace
f2 := ' 7 '::BIGINT
;Result
| f1 BIGINT | f2 BIGINT |
|---|---|
| NULL | 7 |