TRY_CONVERT:- New function in Denali


TRY_CONVERT

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

Syntax

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

SELECTTry_convert (int, ‘1’),
       Try_convert (int, ‘a1’)

If conversion is not allowed it will return error

SELECTTry_convert(image, 123)

use of style

SELECTTry_convert (Varchar(50), Getdate(), 9)

Leave a comment


Design a site like this with WordPress.com
Get started