Conversion functions

SQL Server 2012 introduces some useful conversion functions that will help you avoid errors when dealing with different data types. We will use easy-to-understand examples so you can see how beneficial they are.

TRY_PARSE

TRY_PARSE doesn't look too useful on surface, but as we will soon see it has a vey useful specialist niche.

The basic syntax of TRY_PARSE is as follows:

SELECT TRY_PARSE (string AS datatype)

Each of the following will return a NULL, as the value passed in the string does not convert to a value of the data type specified as the second parameter:

SELECT TRY_PARSE ('SQL Server 2012' AS datetime) AS MyDateTime SELECT TRY_PARSE ('SQL Server 2012' AS decimal) AS MyDecimal SELECT TRY_PARSE ('ABC' AS float) AS MyFloat ...

Get What's New in SQL Server 2012 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.