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 8083455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:28:58+00:00 2026-06-05T17:28:58+00:00

Ok, first question, and you guys scare me with how much you know, so

  • 0

Ok, first question, and you guys scare me with how much you know, so please be gentle…

I am trying to pass in a delimited string and convert it to an array in a stored procedure and then use the array to check against values in a column. Here’s the catch. I’m trying to take the preexisting table, that checks for one association and expand it to allow for multiple associations.

So the column annAssociations might have three ids, 4,16,32, but I need to check if it belongs to the groupIds being queried, 6,12,32. Since one of the values matched, it should return that row.

Here is the procedure as it exists.

CREATE PROCEDURE [dbo].[sp_annList] 
-- Date Range of Announcements.
@dateBegin datetime,
@dateEnd datetime,
    -- Announcement type and associations.
@annType varchar(50),
@annAssociation varchar(255)

AS
BEGIN

-- Set the SELECT statement for the announcements.
SET NOCOUNT ON;
-- See if this should be a limited query
IF @annAssociation <> ''
    Begin
        SELECT * 
        FROM announcements
        WHERE (date1 <= @dateEnd AND date2 >= @dateBegin) -- See if the announcement falls in the date range.
            AND annType = @annType -- See if the announcement is the right type.
            AND annAssociations LIKE (select SplitText from dbo.fnSplit(@annAssociation, ','))
        ORDER BY title
    END
Else 
    Begin
        SELECT *
        FROM announcements
        WHERE (date1 <= @dateEnd AND date2 >= @dateBegin)
            AND annType = @annType
        ORDER BY title
    End
END

And here is the method I’m using to convert the delimited string and store it in a temporary table.

CREATE Function [dbo].[fnSplit](@text text, @delimitor nchar(1))

RETURNS
@table TABLE
(
[Index] int Identity(0,1),
[SplitText] varchar(10)
)
AS

BEGIN
declare @current varchar(10)
declare @endIndex int
declare @textlength int
declare @startIndex int

set @startIndex = 1

if(@text is not null)
begin
    set @textLength = datalength(@text)

    while(1=1)
    begin
        set @endIndex = charindex(@delimitor, @text, @startIndex)

        if(@endIndex != 0)
        begin
            set @current = substring(@text,@startIndex, @endIndex - @StartIndex)
            Insert Into @table ([SplitText]) values(@current)
            set @startIndex = @endIndex + 1   
        end
        else
        begin
            set @current = substring(@text, @startIndex, datalength(@text)-@startIndex+1)
            Insert Into @table ([SplitText]) values(@current)
            break
        end
    end

end

return
END

Sorry for the long question. I just wanted to get all the info out there. I’ve been researching for days, and I either don’t know where to look or am missing something obvious.

  • 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-06-05T17:28:59+00:00Added an answer on June 5, 2026 at 5:28 pm

    You probably won’t get much better performance than this approach (well you might see better performance with a CLR split function but at 3 or 4 items you won’t see much difference):

    SELECT * 
      FROM announcements AS a
      WHERE ...
      AND EXISTS (SELECT 1 FROM dbo.fnSplit(@annAssociation) AS n
        WHERE ',' + a.annList + ',' LIKE '%,' + n.SplitText + ',%');
    

    The key here is that you only need to split up one of the lists.

    You really should stop storing multiple values in the annAssocations column. Each id is a separate piece of data and should be stored separately (in addition to better conforming to normalization, it will make queries like this simpler).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First question on SOF, please be gentle if this may be a stupid question.
i have a question for you guys. im trying to print an array where
Hi guys this is my first question ever on SO so please go easy
Two-headed question here guys, First, I've been trying to do some searching for a
First question on Stackoverflow (.Net 2.0): So I am trying to return an XML
Hey there guys, this is my first question on Stack Overflow. I figured this
Main Question : Ok guys, here is the situation, let's consider 2 string arrays
First of all sorry guys for posting the question here. The support forum seems
Hey guys, the question pretty much asks itself... however, for more clarity: I have
Hi guys this is my first question and i guess can be easy for

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.