I have a variable declared as nvarchar
in the SP, its value is dynamic, it can be a string or a number, its value comes from the UI when user writes in the global search textbox to search a table. I need to cast its value from a string to a decimal only when the value is a number, however, I'm getting error when casting because the cast()
returns error when casting a string like 'abc'
but works fine if the value is '2000'
.
How to check if the value is castable or not?
SELECT CAST('abc' AS DECIMAL(7,2) )
-- retruns error Error converting data type varchar to numeric.
SELECT CAST('2.0000' AS DECIMAL(7,2) )
-- retruns 2.00