I need to find cases where individuals have entered an invalid response in a questionnaire. The list of valid responses is saved as a semicolon-separated string in a table, like this: "starfish;bass;cod;dolphin;shark;"
If I run the query against the text value, it will tell me that "starfish" is not in the list, because I haven't separated the values:
SELECT 'starfish' as value, 'starfish;bass;cod;dolphin;shark;' as 'is not in this list'
WHERE 'starfish' NOT IN ('starfish;bass;cod;dolphin;shark;')
I could go through a fairly manual process where I convert the semicolon list into a quote and comma list. When I do that, it works:
SELECT 'starfish' as value, 'starfish'',''bass'',''cod'',''dolphin'',''shark' as 'is not in this list'
WHERE 'starfish' NOT IN ('starfish','bass','cod','dolphin','shark')
Now, here's where my problem lies: I'd like to save some steps by having my query convert the semicolon string into an apostrophe/comma separated list. However, when I do that, SQL acts as if my value is not in the list.
SELECT 'starfish' as value, (''''+REPLACE(LEFT(RTRIM('starfish;bass;cod;dolphin;shark;'),LEN(RTRIM('starfish;bass;cod;dolphin;shark;'))-1),';',''',''')+'''') as 'is not in this list'
WHERE 'starfish' not in (''''+REPLACE(LEFT(RTRIM('starfish;bass;cod;dolphin;shark;'),LEN(RTRIM('starfish;bass;cod;dolphin;shark;'))-1),';',''',''')+'''')
It appears that SQL doesn't recognize my query-generated list of valid values as a list. Any way to fix?