Skip to content
Advertisement

Passing a varchar full of comma delimited values to a SQL Server IN function

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:

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?

Advertisement

Answer

Don’t use a function that loops to split a string!, my function below will split a string very fast, with no looping!

Before you use my function, you need to set up a “helper” table, you only need to do this one time per database:

use this function to split your string, which does not loop and is very fast:

you can use this function as a table in a join:

Here is your example:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement