In SQL Server Denali, there are three new conversion functions being introduced, namely:
Today we will quickly take a look at the TRY_PARSE() function. The TRY_PARSE() function can convert any string value to Numeric or Date/Time format. If the passed string value cannot be converted to Numeric or Date/Time format, it will result to a NULL.
The PARSE() function relies on Common Language Runtime (CLR) to convert the string value. If there is no CLR installed in the server, the TRY_PARSE() function will return an error. Additionally, please note that TRY_PARSE() only works for String Values to be converted to Numeric and Date/Time. If you need any other data type to be converted in to another data type, you may use the traditional CAST or the Convert Function. As a generic rule, there is always a bit improvement in the performance when any string value is parsed.
Now let us examine these examples showing how TRY_PARSE() works:
Example1: Converting String to INT
-- No error
SELECT PARSE('100.000' AS INT) AS ValueInt;
SELECT TRY_PARSE('100.000' AS INT) AS ValueInt;
SELECT PARSE('A100.000' AS INT) AS ValueInt;
SELECT TRY_PARSE('A100.000' AS INT) AS ValueInt;
When you try first set where string can be converted to INT, it does not throw an error and it works fine.
However, in the second case wherein the string has an Alphabetic character, it cannot be converted to INT. In this case, PARSE will throw an error but TRY_PARSE will return a NULL result. Therefore, the major difference between PARSE and TRY_PARSE is that TRY_PARSE will TRY to PARSE and if PARSE fails it will produce a NULL result instead of an error.
This distinct attribute separating the two functions are very helpful when we are using TRY_PARSE function over table. This is displayed in the examples below:
Example2: Using TRY_PARSE with CASE statement.
CASE WHEN TRY_PARSE('A100.000' AS INT) IS NULL
THEN 'Error In Result'
ELSE 'No Error'--TRY_PARSE('A100.000' AS INT)
END AS ValueInt;
The example above demonstrates how CASE statement can be used with TRY_PARSE, while avoiding any errors in the statement execution.
In the following example, TRY_PARSE is applied over table and it makes it more meaningful:
Example3: Converting String to INT from Table using TRY_PARSE
You can clearly see that the PostalCode column in the database is in the format of the nvarchar. We can use PARSE to convert it to INT.
SELECT SP.[StateProvinceCode], A.[PostalCode],
TRY_PARSE(A.[PostalCode] AS INT) PostCode_TryParse
FROM [Person].[Address] A
INNER JOIN [Person].[StateProvince] SP
ON SP.StateProvinceID = A.StateProvinceID
When attempting to PARSE a column, you might get one or more incorrect values. If the PARSE function is used it will throw an error and will not return any results but with TRY_PARSE it will not throw error but it will return the NULL value. As seen in example, the NULL value can be used along with CASE and desired message can be displayed.
In the next blog post, we will see the usage of TRY_CONVERT() and there we will see the real difference of PARSE and TRY_PARSE.
Reference: Pinal Dave (http://blog.SQLAuthority.com)