Duplicate of
Dynamic SQL Comma Delimited Value Query
Parameterized Queries with Like and In
I have a SQL Server Stored Procedure where I would like to pass a varchar
full of comma delimited values to an IN
function. For example:
DECLARE @Ids varchar(50);
SET @Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE tableid IN (@Ids);
This does not work of course. I get the error:
Conversion failed when converting the varchar value '1,2,3,5,4,6,7,98,234' to data type int.
How can I accomplish this (or something relatively similar) without resorting to building dynamic SQL?
I can suggest using
WITH
like this:You could do it like this:
Then try it with
You can use this IN-Clause with regexp_substr in other situations too, just try it.
working fiddle find here Fiddle
Of course if you're lazy like me, you could just do this:
I have same idea with user KM. but do not need extra table Number. Just this function only.
And here is the test:
Result: