This is the new function introduced with 2016 version of SQL Server. This is used to cast value from its existing data type to a specified target data type. This occurs only if the operation is successful. It returns NULL if the conversion fails.
TRY_CAST is an extended version of the CAST function.
Query
TRY_CAST is an extended version of the CAST function.
Query
SELECT CAST('Tech-Recipes' as INT) as CastExample;
result
Msg 245, Level 16, State 1, Line 2
Conversion failed when
converting the varchar value 'Tech-Recipes' to data
type int.
query
SELECT CAST('12' as INT) as CastExample;
result
12
SELECT TRY_CAST('Tech-Recipes' as INT) as CastExample;
Result
NULL
SELECT TRY_CAST('20' as INT) as CastExample;
Result
20
Comments
Post a Comment
Plz dont forget to like Facebook Page..
https://www.facebook.com/pages/Sql-DBAcoin/523110684456757