RSS Feed

TRY_CONVERT()

TRY_CONVERT()

TRY_CONVERT(): One new function for casting has been introduced in SQL Server 2012 in which the input value will be casted to specified data type if the cast succeeds else it will return null. TRY_CONVERT, a new keyword which has compatibility level 110.

Syntax of TRY_CONVERT():

TRY_CONVERT (data_type [ ( length ) ], expression [, style ] )

Example of TRY_CONVERT():

IF TRY_CONVERT(INTEGER, 'one') IS NULL 
BEGIN
    PRINT 'NULL'
END
    ELSE 
BEGIN
    PRINT 'NOT NULL'
END
The output will be NULL.

For complete post go to SQL Server 2012 Enhancements.