Skip to main content
Converts data types into other data types based on the specified parameters. If the conversion cannot be performed, returns a NULL. To return an error message instead, use CAST.
TRY_CAST replaces only execution errors with NULLs. However, during planning, impossible casts between two non-castable types still produce an error because the query is invalid.

Syntax

Parameters

Return Type

Returns NULL if the conversion cannot be performed. Otherwise, returns the data type of <type>. When <type> is an array or a struct, each element or field is converted on its own: one that cannot be converted becomes NULL while the rest of the value is kept. This applies at every level of nesting. For an array, the whole result is NULL only when <value> is NULL or is text that is not an array literal at all.

Example

The following example attempts to cast the level input as an integer:

Rows: 1Execution time: 2.87ms

The following example converts an array element by element. The element that is not a number becomes NULL:

Rows: 1Execution time: 1.19ms