SQL Server Denali CTP3 has introduces a new function TRY_CONVERT() to evaluates whether a data type conversion is possible or not?
TRY_CONVERT(), checks whether typecasting / datatype conversion from one datatype to other datatype is possible or not and if it possible then it converts the source data type into the target type and if the conversion is not possible then it returns NULL.
How to use TRY_CONVERT() in SQL Server ?
In above example, we tried to covert a string ‘test’ into float, which is not possible and NULL is returned by TRY_CONVERT().
-- Another Example of TRY_CONVERT() Select Try_Convert(DateTime,'19910902') As DATE_TIME_CONVERSION Select Try_Convert(Numeric(10,4),'19910902') As DECIMAL_CONVERSION
result from the above query
Result ----------- DATE_TIME_CONVERSION 1991-09-02 Result ----------- DECIMAL_CONVERSION 19910902.0000
