I've got the following bit of code as part of a SQL Query:
INSERT INTO [Database]
SELECT DISTINCT @ssId
FROM [Document_Map]
WHERE (LabelId IN (SELECT Tokens
FROM StringSplitter(@sortValue, '|', 1))
The code works fine as long as @SortValue
is an integer, (LabelId
is an int as well) or integers separated by the delimiter character (e.g., SortValue 420|421| compares against 420 and 421). However, I'm adding functionality which involves non-integer values for @sortValue
. E.g.: 420|ABC|421|
should compare against 420, ABC, and 421.
No worries, I thought. I'll just modify the last line of that query!
(LabelId IN (SELECT Tokens FROM StringSplitter(@sortValue, '|', 1)) OR
StringId IN (SELECT Tokens FROM StringSplitter(@sortValue, '|', 1)))
Unfortunately, whenever I feed in @sortValue
with characters in it, I get an error. It never made it to the far side of the or
.
After much mucking around, I finally determined that SQL is casting the string results from the StringSplitter function to compare against LabelId. No problem when the string results were guaranteed to contain only numeric characters, but SQL refuses to cast (understandably) the non-numeric string to an int, throwing out an error.
What's the easiest way to get around this error while maintaining desired functionality? Due to database interaction, I am not sure if changing the type of LabelId
is a viable option.