TRY_CONVERT:- New function in Denali


Tries to convert data type to a specified data type and if fails returns Null


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

data_type:- data_type in which we want to convert data
expression:- expression which will be converted to speicified data_type
style:-specified style, A integer number in which data will be converted. 
Its same as we specified in convert

Lets see example to understand it

If conversion is possible it will be return converted value else null

SELECT Try_convert (int, '1'),
       Try_convert (int, 'a1')

If conversion is not allowed it will return error

SELECT Try_convert(image, 123)

use of style

SELECT Try_convert (Varchar(50), Getdate(), 9)

No comments:

Post a Comment

Popular Posts