There is an instance where the filters I have are being dynamically plugged into my SQL query and sometimes cannot tell prior if they are int
or varchar
. Is there a way to allow either with a CASE WHEN
statement?
In my table (ent
for entity), the column company is a string/varchar
and the entid
column is an int
. I cannot seem to get this CASE IsNumeric
statement to work for cases when an int is the filter, or the string is the filter.
This works:
select
ent.company
from
ent
where
ent.company in
(CASE
WHEN IsNumeric(CONVERT(VARCHAR(100), 'ABIS, Inc.')) = 0
THEN CONVERT(VARCHAR(100), 'ABIS, Inc.')
ELSE '99'
END,
CASE
WHEN IsNumeric(CONVERT(VARCHAR(100), 'Schulte Building Systems')) = 0
THEN CONVERT(VARCHAR(100), 'Schulte Building Systems')
ELSE '1423'
END)
This is not working:
select
ent.company
from
ent
where
ent.entid in
(CASE
WHEN IsNumeric(CONVERT(VARCHAR(100), 'ABIS, Inc.')) = 0
THEN CONVERT(VARCHAR(100), 'ABIS, Inc.')
ELSE '99'
END,
CASE
WHEN IsNumeric(CONVERT(VARCHAR(100), 'Schulte Building Systems')) = 0
THEN CONVERT(VARCHAR(100), 'Schulte Building Systems')
ELSE '1423'
END)
...but for the int equals if I change the 0's to 1's it works (and if I use company equals, it returns nothing). Any idea why it's failing?