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?
Best and simple approach.
You can create a function that returns a table.
so your statement would be something like
Here is a simular function.
Create a table function like below which parse comma separated varchar and returns a table that can be inner joined with other tables.
================================================= Now consume above created table function in your code,creation of function is one time activity in your database that can be used across databases as well on same server.
The above errorr was fixed in SQL Server 2014 by using following snippet
Thanks, for your function I Used IT........................ This is my EXAMPLE
The simplest way i found was to use FIND_IN_SET
FIND_IN_SET(column_name, values)
values=(1,2,3)
SELECT name WHERE FIND_IN_SET(id, values)