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

  • Home
  • SEARCH
  • 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 6250391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:24:54+00:00 2026-05-24T13:24:54+00:00

I have a large T-SQL query (I only put a part here because I

  • 0

I have a large T-SQL query (I only put a part here because I very sure the rest is working nice):

 WHERE /*
                ******* missing filter *******
                this line should filter the risks by @LoggedInPersonID via role 
                ******************************
          AND */(@PropertyID IS NULL OR p.PropertyID = @PropertyID)
          AND (@PCodePattern IS NULL OR p.PCode LIKE @PCodePattern)
          AND (@ZipCodeIDS IS NULL 
                 OR p.ZipCodeID IN (@ZipCodeIDS))

You notice that @ZipCodeIDS is a list… Well, I populate this from code with some ids. When @ZipCodeIDS contains a SINGLE id it works perfectly but if I try to send multiple ids (eg: “14,15”) it gives me error….

Do you have any clue?

Ps: Of course I made @ZIpCodeIDS varchar because we do not have array in T-SQL…

UPDATE: If I directly hardcode in the query: IN (14,11) it perfectly works… so somehow I do not correctly send this parameter from my code for multiple values I think it does not see “,” as a separator…

UPDATE 2: Tried to split the lists and put the ids into a table… When zipCodeIDS has only one id the query works perfectly. For multiple ones, not… Do you see something strange on ELSE?

    IF @ZipCodeIDS IS NOT NULL
BEGIN
    IF CHARINDEX(',', @ZipCodeIDS) = 0
    BEGIN
        insert @listofIDS values(@ZipCodeIDS)
    END
    ELSE
    BEGIN
        WHILE CHARINDEX(',', @ZipCodeIDS) != 0
        BEGIN
            insert @listofIDS values(Left(@ZipCodeIDS, PatIndex(',', @ZipCodeIDS) - 1))
            SET @ZipCodeIDS = SUBSTRING(@ZipCodeIDS, CHARINDEX(',',@ZipCodeIDS)+1, LEN(@ZipCodeIDS) - CHARINDEX(',',@ZipCodeIDS))
        END
    END
END
  • 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-24T13:24:55+00:00Added an answer on May 24, 2026 at 1:24 pm
    Declare @listofIDS table(zipcodeids varchar(10))
    
    Declare @ZipCodeIDS varchar(100)
    set  @ZipCodeIDS= '14,15'
    
    set @ZipCodeIDS+= ','
    ;with cte as
    (
    select substring(@ZipCodeIDS, 1, charindex(',', @ZipCodeIDS) - 1) num, charindex(',', @ZipCodeIDS) pos
    union all
    select substring(@ZipCodeIDS, pos + 1, charindex(',', @ZipCodeIDS, pos +1) - pos - 1) num, charindex(',', @ZipCodeIDS, pos +1)
    from cte
    where charindex(',', @ZipCodeIDS, pos +1) > 0
    
    )
    INSERT @listofIDS 
    select num from cte
    
    
    .... or p.ZipCodeID in (select zipcodeids from @listofIDS) 
    
    --------------------
    

    Try this (remember to set all your variables):

    Declare @ZipCodeIDS varchar(100)
    set  @ZipCodeIDS= '14,15'
    
    set @ZipCodeIDS+= ','
    ;with cte as
    (
    select cast(substring(@ZipCodeIDS, 1, charindex(',', @ZipCodeIDS) - 1) as int) num, charindex(',', @ZipCodeIDS) pos
    union all
    select cast(substring(@ZipCodeIDS, pos + 1, charindex(',', @ZipCodeIDS, pos +1) - pos - 1) as int) num, charindex(',', @ZipCodeIDS, pos +1)
    from cte
    where charindex(',', @ZipCodeIDS, pos +1) > 0
    )
    
    SELECT /*... rest of your query that was not included in the question. no colon, nor anything else, just your query plus the small alteration i made in the end
    ...
    ...*/
    WHERE /*
                    ******* missing filter *******
                    this line should filter the risks by @LoggedInPersonID via role 
                    ******************************
              AND */(@PropertyID IS NULL OR p.PropertyID = @PropertyID)
              AND (@PCodePattern IS NULL OR p.PCode LIKE @PCodePattern)
              AND (@ZipCodeIDS IS NULL 
                     OR p.ZipCodeID IN (select num from cte))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it particularly bad to have a very, very large SQL query with lots
SQL Server 2008. I have this very large query which has a high cost
We have a SQL query that pulls a large number of fields from many
Hello I have a very large SQL script and while trying to execute it
I have an app that needs to handle very large strings between a SQL
I was reading about refactoring a large slow SQL Query over here , and
I have a SQL query that takes a very long time to run on
We have a large-ish query here that has several params, and for each one,
I have large size of update query it is 320 Kb sql file. i
I have a rather large SQL file which starts with the byte order marker

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.