Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 792139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:56:46+00:00 2026-05-14T21:56:46+00:00

How can i pass a string delimited by space or comma to stored procedure

  • 0

How can i pass a string delimited by space or comma to stored procedure and filter result?
I’m trying to do something like –

Parameter      Value
--------------------------
@keywords      key1 key2 key3

Then is stored procedure i want to first

  1. find all records with first or last
    name like key1
  2. filter step 1 with first or last
    name like key2
  3. filter step 2 with first or last name like key 3

Another example:

col1    |       col2        | col3
------------------------------------------------------------------------
hello xyz   |   abc is my last name | and i'm a developer
hello xyz   |       null        | and i'm a developer

If i search for any following it should return for each?

  1. “xyz developer” returns 2 rows

  2. “xyz abc” returns 1 row

  3. “abc developer”returns 1 row

  4. “hello” returns 2 rows

  5. “hello developer” returns 2 rows

  6. “xyz” returns 2 rows

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:56:47+00:00Added an answer on May 14, 2026 at 9:56 pm

    Since you can’t use a table parameter (not on SQL Server 2008), try passing in a CSV sting and have the stored procedure split it into rows for you.

    There are many ways to split string in SQL Server. This article covers the PROs and CONs of just about every method:

    “Arrays and Lists in SQL Server 2005 and Beyond, When Table Value Parameters Do Not Cut it” by Erland Sommarskog

    You need to create a split function. This is how a split function can be used:

    SELECT
        *
        FROM YourTable                               y
        INNER JOIN dbo.yourSplitFunction(@Parameter) s ON y.ID=s.Value
    

    I prefer the number table approach to split a string in TSQL but there are numerous ways to split strings in SQL Server, see the previous link, which explains the PROs and CONs of each.

    For the Numbers Table method to work, you need to do this one time table setup, which will create a table Numbers that contains rows from 1 to 10,000:

    SELECT TOP 10000 IDENTITY(int,1,1) AS Number
        INTO Numbers
        FROM sys.objects s1
        CROSS JOIN sys.objects s2
    ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
    

    Once the Numbers table is set up, create this split function:

    CREATE FUNCTION [dbo].[FN_ListToTable]
    (
         @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
        ,@List     varchar(8000)--REQUIRED, the list to split apart
    )
    RETURNS TABLE
    AS
    RETURN 
    (   ----------------
        --SINGLE QUERY-- --this will not return empty rows
        ----------------
        SELECT
            ListValue
            FROM (SELECT
                      LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                      FROM (
                               SELECT @SplitOn + @List + @SplitOn AS List2
                           ) AS dt
                          INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                      WHERE SUBSTRING(List2, number, 1) = @SplitOn
                 ) dt2
            WHERE ListValue IS NOT NULL AND ListValue!=''
    );
    GO 
    

    You can now easily split a space delimited string into a table and join on it or use it however you need This codes is based on the OPs latest question edit:

    CREATE TABLE YourTable (PK int, col1 varchar(20), col2 varchar(20), col3 varchar(20))
    --data from question
    INSERT INTO YourTable VALUES (1,'hello xyz','abc is my last name','and i''m a developer')
    INSERT INTO YourTable VALUES (2,'hello xyz',null,'and i''m a developer')
    
    CREATE PROCEDURE YourProcedure
    (
        @keywords   varchar(1000)
    )
    AS
    
    SELECT
        @keywords AS KeyWords,y.* 
        FROM (SELECT
                  t.PK
                  FROM dbo.FN_ListToTable(' ',@keywords) dt
                      INNER JOIN YourTable             t ON  t.col1 LIKE '%'+dt.ListValue+'%' OR t.col2 LIKE '%'+dt.ListValue+'%' OR t.col3 LIKE '%'+dt.ListValue+'%'
                  GROUP BY t.PK
                  HAVING COUNT(t.PK)=(SELECT COUNT(*) AS CountOf FROM dbo.FN_ListToTable(' ',@keywords))
             ) dt
            INNER JOIN YourTable y ON dt.PK=y.PK
    GO
    
    --from question   
    EXEC YourProcedure 'xyz developer'-- returns 2 rows
    EXEC YourProcedure 'xyz abc'-- returns 1 row
    EXEC YourProcedure 'abc developer'-- returns 1 row
    EXEC YourProcedure 'hello'--  returns 2 rows
    EXEC YourProcedure 'hello developer'--  returns 2 rows
    EXEC YourProcedure 'xyz'-- returns 2 rows
    

    OUTPUT:

    KeyWords       PK    col1       col2                 col3
    -------------- ----- ---------- -------------------- --------------------
    xyz developer  1     hello xyz  abc is my last name  and i'm a developer
    xyz developer  2     hello xyz  NULL                 and i'm a developer
    
    (2 row(s) affected)
    
    KeyWords       PK    col1       col2                 col3
    -------------- ----- ---------- -------------------- --------------------
    xyz abc        1     hello xyz  abc is my last name  and i'm a developer
    
    (1 row(s) affected)
    
    KeyWords       PK    col1       col2                 col3
    -------------- ----- ---------- -------------------- --------------------
    abc developer  1     hello xyz  abc is my last name  and i'm a developer
    
    (1 row(s) affected)
    
    KeyWords       PK    col1       col2                 col3
    -------------- ----- ---------- -------------------- --------------------
    hello          1     hello xyz  abc is my last name  and i'm a developer
    hello          2     hello xyz  NULL                 and i'm a developer
    
    (2 row(s) affected)
    
    KeyWords        PK    col1       col2                 col3
    --------------- ----- ---------- -------------------- --------------------
    hello developer 1     hello xyz  abc is my last name  and i'm a developer
    hello developer 2     hello xyz  NULL                 and i'm a developer
    
    (2 row(s) affected)
    
    KeyWords       PK    col1       col2                 col3
    -------------- ----- ---------- -------------------- --------------------
    xyz            1     hello xyz  abc is my last name  and i'm a developer
    xyz            2     hello xyz  NULL                 and i'm a developer
    
    (2 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a format string (?) I can pass to the VB6 Format function
Using logparser you can pass on parameters to the query you'd like to run
When I submit a form in HTML, I can pass a parameter multiple times,
It appears the query string parameters you can pass to a rest method in
You can pass a function pointer, function object (or boost lambda) to std::sort to
I was wondering how I can pass either an ArrayList, List <int > or
In a vxWorks Real-Time process, you can pass environment variables as one of the
When I render a page using the Django template renderer, I can pass in
When using the Ajax.BeginForm() helper in ASP.Net MVC , I can pass options with
Can I pass this to a function as a pointer, from within the class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.