web analytics

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?

You use Microsoft SQL Server 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?
A. BEGIN TRY
SELECT
convert (decimal(36,9), @var) as Value,
‘True’ As BadCast
END TRY
BEGIN CATCH
SELECT
CONVERT (DECIMAL(36,9), @var) as Value,
‘False’ As BadCast
END CATCH
B. TRY(
SELECT CONVERT (DECIMAL(36,9), @var)
SELECT ‘True’ As BadCast
)
CATCH(
SELECT ‘False’ As BadCast
)
SELECT
C. CASE
WHEN CONVERT (DECIMAL(36,9), @var) IS NULL
THEN ‘True’
ELSE ‘False’
END
AS BadCast
D. SELECT
IIF(TRY_PARSE(@var AS decimal(36,9)) IS NULL,
‘True’,
‘False’
)
AS BadCast
Answer: D