In this post explains how to use the TRY_CAST function in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server (Transact-SQL), the TRY_CAST function tries to convert an expression from one datatype to another datatype. If the conversion fails, the function will return NULL. Otherwise, it will return the converted value.
The syntax for the TRY_CAST function in SQL Server (Transact-SQL) is:
The value to convert to another datatype.
The datatype that you wish to convert expression to. It can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image.
Optional. The length of the resulting data type for char, varchar, nchar, nvarchar, binary and varbinary.
The TRY_CAST function can be used in the following versions of SQL Server (Transact-SQL):
Let's look at some SQL Server TRY_CAST function examples and explore how to use the TRY_CAST function in SQL Server (Transact-SQL).
For example: