I'm using SQL Server 2008
I have a column called varEnteredValue of datatype sql_Variant.
When I execute the following select statement I don't get anything
SELECT * FROM tblname WHERE varEnteredValue = 1
Notice I have many values with value 1 in this column
Question
How can I fix this issue? Do I need to convert data type ?
There are two problems here:
SQL_VARIANT to INT.INT, and it can be difficult to predict whether SQL Server will filter first or try to convert first. You need to test first if the value is numeric, and in order to do that, you must explicitly convert it to a string.Here is an example that works around both issues:
WHERE CASE WHEN ISNUMERIC(CONVERT(VARCHAR(32), varEnteredValue)) = 1
THEN CONVERT(FLOAT, varEnteredValue) ELSE 0 END = 1;
Why are you using SQL_VARIANT?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With