TRY_CAST Function - SQL


Overview


The TRY_CAST function in SQL attempts to convert an expression to a specified data type. If the conversion is successful, it returns the converted value; otherwise, it returns NULL.

Example:

SELECT TRY_CAST(10 AS CHAR(2))

Syntax:

SELECT TRY_CAST(expression AS target_data_type)

expression is the value to convert from one data type to another.

target_data_type is the data type to which you want to convert the expression.