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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:29:18+00:00 2026-05-30T16:29:18+00:00

I am trying to find out what’s the most optimized SQL Query to achieve

  • 0

I am trying to find out what’s the most optimized SQL Query to achieve the following.

I have a table containing ZipCodes/PostalCodes, let’s assume the following structure:

table_codes:

ID   |  ZipCode
---------------
1       1234
2       1235
3       456

and so on.

The users of my application fill up a profile where they are required to enter their ZipCode (PostalCode).
Assuming that sometimes, the user will enter a ZipCode not defined in my table, I am trying to suggest a Best Match based on the zip entered by the user.

I am using the following query:

Declare @entered_zipcode varchar(10)
set @entered_zipcode = '23456'


SELECT TOP 1 table_codes.ZipCode
FROM    table_codes
where   @entered_zipcode  LIKE table_codes.ZipCode + '%'
or table_codes.ZipCode + '%' like @entered_zipcode  + '%'
ORDER BY table_codes.ZipCode, LEN(table_codes.ZipCode) DESC

Basically, I am trying the following:

  • if the @entered_zipcode is longer than any zip code in the table, I am trying to get to get the best prefix in the zip table matching the @entered_zipcode

  • if the @entered_zipcode is shorter than any existing code in the table, I am trying to use it as a prefix and get the best match in the table

Moreover, I am building a temp table with the following structure:

#tmpTable
------------------------------------------------------------------------------------
ID | user1_enteredzip | user1_bestmatchzip | user2_enteredzip | user2_bestmatchzip |
------------------------------------------------------------------------------------
1  |   12             |     *1234*         |       4567       |       **456**      |
2  |
3  |
4  |

Entered zip is the one the user enters and the code between * .. * is the best matching code from my lookup table, that I am trying to get using the query below.

The query seems to take a little bit to long and this is why I am asking for help in optimizing it:

        update  #tmpTable
        set     user1_bestmatchzip = ( SELECT TOP 1
                                            zipcode
                                    FROM    table_codes
                                    where   #tmpTable.user1_enteredzip  LIKE table_codes.zipcode + '%'
                                            or table_codes.zipcode + '%' like #tmpTable.user1_enteredzip + '%'
                                    ORDER BY table_codes.zipcode, LEN(table_codes.zipcode) DESC
                                  ),
                user2_bestmatchzip = ( SELECT TOP 1
                                            zipcode
                                    FROM    table_codes
                                    where   #tmpTable.user2_enteredzip  LIKE table_codes.zipcode + '%'
                                            or table_codes.zipcode + '%' like #tmpTable.user2_enteredzip + '%'
                                    ORDER BY table_codes.zipcode, LEN(table_codes.zipcode) DESC
                                  )
         from #tmpTable
  • 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-30T16:29:19+00:00Added an answer on May 30, 2026 at 4:29 pm

    What if you change your temp table to be like:

    id  |  user | enteredzip | bestmatchzip 
    10  |  1    | 12345      | 12345
    20  |  2    | 12         | 12345
    

    That is: use a column to save the user number (1 or 2). This way you will update one row at a time.

    Also, the ORDER BY takes time, did you set indices on the zipcode? Couldn’t you create a field “length” in the zipcodes table to pre-compute the zipcodes lenghts?

    EDIT:
    I was thinking that ordering by LEN makes no sense, you could remove that! If the zipcodes cannot have duplicates, then ordering by the zipcode is just enought. If they can though, the LEN will always be equal!

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

Sidebar

Related Questions

I am trying to find out why the following occurs if you have $arr
I'm trying to find out the most efficient (best performance) way to check date
I've been trying to find out if the following scenario is possible, I'll do
Im trying to find out a good JavaScript library that can create a nice
I'm trying to find out how much memory my own .Net server process is
I'm trying to find out if there is any way to elevate a specific
I'm trying to find out the 'correct' windows API for finding out the localized
I'm trying to find out whether I should be using business critical logic in
I am trying to find out how to upload a file from a web
I am trying to find out how much memory my application is consuming from

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.