I have a query that needs to compare a value with multiple columns and only return results from one. I keep getting a conversion error when it tries to convert a string argument to an integer, but I could not find a good way around this.
SELECT DISTINCT
CASE
WHEN table_one.integer_col = CAST('argument%' AS int)
THEN table_one.integer_col
WHEN table_one.varchar_col LIKE 'argument%'
THEN table_one.varchar_col
WHEN table_two.varchar_col LIKE 'argument%'
END
FROM table_one
INNER JOIN table_two
ON table_one.pk=table_two.fk
WHERE
table_one.integer_col = CAST('argument%' AS int)
table_one.varchar_col LIKE 'argument%' OR
table_two.varchar_col LIKE 'argument%'
This works when the "argument%" can be successfully converted to an integer, but when this is not possible, the request is bombed.
source
share