TRY_CAST

All functions > CORE > TRY_CAST

Returns the given expression casted to the given type, or NULL if the cast fails.

Signatures

Returns: The expression converted to the specified type, or NULL if conversion fails

TRY_CAST(expr: T, as_type: DataTypeEnum.TYPE) → U
sql
ParameterTypeRequiredDescription
exprTYesThe expression to convert
as_typeDataTypeEnum.TYPEYesThe target data type

Notes

  • Converts a value from one data type to another
  • Returns NULL instead of raising an error when the conversion fails
  • Supports the same conversions as CAST
  • Safe alternative to CAST when the input may contain invalid values
  • NULL values remain NULL after casting

Examples

FeatureQL
SELECT
    f1 := TRY_CAST('123' AS BIGINT),  -- Valid string to integer
    f2 := TRY_CAST('abc' AS BIGINT),  -- Invalid string returns NULL
    f3 := TRY_CAST(42 AS DOUBLE),  -- Integer to double
    f4 := TRY_CAST('2024-01-15' AS DATE),  -- Valid string to date
    f5 := TRY_CAST('not-a-date' AS DATE)  -- Invalid date string returns NULL
;
Result
f1 BIGINTf2 BIGINTf3 VARCHARf4 TIMESTAMPf5 TIMESTAMP
123NULL42.02024-01-15NULL

Last update at: 2026/03/03 16:47:38
Last updated: 2026-03-03 16:48:19