The TRY expression ensures that errors caused by the input rows in the child (scalar) expression result in NULL for those rows, instead of causing the query to throw an error.
The
TRYexpression was inspired by theTRY_CASTexpression.
Examples
The following calls return errors when invoked without the TRY expression.
When they are wrapped into a TRY expression, they return NULL:
Casting
Without TRY
SELECT 'abc'::INTEGER;
Conversion Error:
Could not convert string 'abc' to INT32
With TRY
SELECT TRY('abc'::INTEGER);
NULL
Integer Division by Zero
Without TRY
SELECT 1 // 0;
Invalid Input Error:
Division by zero in expression (1 // 0). Use TRY(...) to return NULL for this expression, or SET error_on_division_by_zero=false to return NULL for all divisions by zero.
With TRY
SELECT TRY(1 // 0);
NULL
Casting Multiple Rows
Without TRY
WITH cte AS (FROM (VALUES ('123'), ('test'), ('235')) t(a))
SELECT a::INTEGER AS x FROM cte;
Conversion Error:
Could not convert string 'test' to INT32
With TRY
WITH cte AS (FROM (VALUES ('123'), ('test'), ('235')) t(a))
SELECT TRY(a::INTEGER) AS x FROM cte;
| x |
|---|
| 123 |
| NULL |
| 235 |
Limitations
TRY cannot be used in combination with a volatile function, an aggregate function, or a scalar subquery.
For example:
SELECT TRY(random())
Binder Error:
TRY can not be used in combination with a volatile function
関連キーワード
関連するページ
© 2026 DuckDB Foundation, Amsterdam NL