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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:44:29+00:00 2026-05-20T23:44:29+00:00

I am designing a stored procedure to return results from a search. I need

  • 0

I am designing a stored procedure to return results from a search.

I need all apartments returned that has a specific zipcode..

The select should do what this does:

select * from tbl_houses
where

(
  (zip_code in (10000,20000,30000,40000))
  or
  (zip_code > 50000 and zip_code < 60000)
)

In other words. I want a stored procedure which can both take a zip_code range and series of zip_codes.

Anyone know an elegant way of doing this?

My current solutions are ugly as xxxx.

Cheers
Mathias

  • 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-20T23:44:30+00:00Added an answer on May 20, 2026 at 11:44 pm

    You can use a Split Table-value function for this:

    CREATE FUNCTION [dbo].[Split](@RowData VARCHAR(8000), @SplitOn VARCHAR(5))  
    RETURNS @RtnValue TABLE 
    (
        Id int identity(1,1),
        Data nvarchar(2000)
    ) 
    AS  
    BEGIN 
        Declare @Cnt int
        Set @Cnt = 1
    
        While (Charindex(@SplitOn,@RowData)>0)
        Begin
            Insert Into @RtnValue (data)
            Select 
                Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
    
            Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
            Set @Cnt = @Cnt + 1
        End
    
        Insert Into @RtnValue (data)
        Select Data = ltrim(rtrim(@RowData))
    
        Return
    END
    

    Then you can use it on your stored procedure, maybe with another parameter indicating if its a range or a list:

    CREATE PROCEDURE dbo.GetHouses(@Zip VARCHAR(8000), @Type INT)
    AS
    /* 
      @Type:    1 - List
                2 - Range
    */
    
    IF @Type = 1
    BEGIN
        SELECT * 
        FROM tbl_houses H
        JOIN dbo.Split(@Zip, ',') S
        ON H.zip_code = S.Data
    END
    IF @Type = 2
    BEGIN
        SELECT * 
        FROM tbl_houses
        WHERE zip_code >= (SELECT Data FROM dbo.Split(@Zip, ',') WHERE Id  = 1)
        AND zip_code <= (SELECT Data FROM dbo.Split(@Zip, ',') WHERE Id  = 2)
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

About best practices on designing stored procedures, should a stored procedure updating a record
I'm told that when designing stored procedures, set based operations scale better than cursor
I am designing a web application that retrieves records from a database according to
I have the following stored procedure that I am trying to convert to a
I am designing an app in which the data is stored in the database.
I'm designing a table that will be used to store information on which customers
I'm designing a Java based web-app and I need a key-value store. Berkeley DB
I am designing a new laboratory database. I want to store the raw results
Designing forms has always been fun, but getting them to send email on the
I can't for the life of me figure out how this stored procedure works

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.