Delimited List Of Items As input parameter

Hi All,

I'm sending a delimited string value to my stored proc as an input parameter. It looks like this ('1;2;3;4;5').
I have function that will I call inside a while loop that will extract each value. I'm stuck with the following problem.
I want to write a query that will select items from a table that will not include values from my delimted list. For example,
I have a table with 10 records with unique id's from 1 to 10. If I use my delimited list from above, I want to only retrieve records
from id's 6-10 and exclude 1- 5.  How do I go about doing this in my proc.  Thanks,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

after you've inserted the passed parms into a temp table

use the EXISTS/NOT Exists clause


Select a.* from mytable as a
where not exists (select from #temp where =

or if you're using dynamic sql then

set @sql = 'select a.* from mytable as a where not in (''' + Replace(@parm,';','''')+''')'



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian CroweDatabase AdministratorCommented:
Here is a function that will parse the list into a table for you

CREATE FUNCTION ParseList(@List varchar(8000), @Delim char(1))
      Rank int IDENTITY(1,1),
      ListItem varchar(100)
DECLARE @Item varchar(100)
DECLARE @Index int

WHILE LEN(@List) > 0
      SET @Index = CHARINDEX(@Delim, @List)
      IF @Index = 0
            SET @Item = RTRIM(LTRIM(@List))
            SET @List = ''
            SET @Item = RTRIM(LTRIM(SUBSTRING(@List, 1, @Index - 1)))
            SET @List = SUBSTRING(@List, @Index + 1, LEN(@List))
      INSERT INTO @ListItems (ListItem) VALUES (@Item)

After you add this function to your database use it like such...

   @parm varchar(100)

FROM myTable
LEFT OUTER JOIN dbo.ParseList(@parm, ';') AS ListItems
   ON myTable.ID = ListItems.ListItem
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.