SQL Server Denali CTP3 has introduces a new function TRY_PARSE() to translate the result of an expression to specified data type.
TRY_PARSE() tries to translate the result of an expression to specified data type if the translation is possible, otherwise, it returns NULL. Moreover, we can also specify the CULTURE, during this conversion.
CULTURE, means a language (English, Japanese, Spanish, Danish, French etc.) which will used by SQL Server to interpret data.
HOW to use TRY_PARSE() in SQL Server ?
another example,
SELECT TRY_PARSE('18' AS datetime2) -- Returned NULL GO SELECT TRY_PARSE('18' AS NUMERIC(18,2)) -- Returned 18.00 GO
By using this TRY_PARSE() function, we can avoid errror, "Conversion failed when converting the varchar value ‘DBATAG’ to data type int.", where we are unsure about the values.
NOTE :
- TRY_PARSE is only used for converting from string to date/time and number types.
- If the server does not have the CLR installed, this function will fail
For More Information check Microsoft BOL
