Querying Microsoft SQL Server 2012/2014 — Question 12

You use Microsoft SQL Server 2012 to develop a database application.
Your application sends data to an NVARCHAR(MAX) variable named @var.
You need to write a Transact-SQL statement that will find out the success of a cast to a decimal (36,9).
Which code segment should you use?

Answer options

Correct answer: D

Explanation

The correct answer is D because TRY_PARSE is designed to attempt a conversion and returns NULL if it fails, making it ideal for this situation. Option A incorrectly uses a TRY...CATCH block that does not directly address the conversion success check, while B is syntactically incorrect. Option C uses a CASE statement which does not appropriately handle the error in conversion as it doesn’t utilize a TRY...CATCH mechanism.