Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

search

Posted on 1998-10-08
2
Medium Priority
?
245 Views
Last Modified: 2010-03-19
I was wondering if anyone could help me with an SQL call.  A user is going to input a zip code, let's say 44236.  I need a call that will look for an exact match if no match then find the closest match.  So if 44255 is in the data base the data on that number (44255) will be displayed.

TIA
0
Comment
Question by:csindorf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
gertrude earned 180 total points
ID: 1090455
If you've defined your zip code column as a character datatype you can do something like the following example that works in Pubs:

  USE Pubs

  DECLARE @iRecCount int
  DECLARE @cZip char(5)

  SET NOCOUNT ON

  SELECT @cZip = '94300'

  --** Search for exact match
  SELECT @iRecCount = count(*) FROM Authors WHERE Zip = @cZip

  IF @iRecCount > 0
    BEGIN
      --** Return the exact match
      SET ROWCOUNT 1
      SELECT Match='Exact', Zip FROM Authors WHERE Zip = @cZip
      SET ROWCOUNT 0
    END
  ELSE
    BEGIN
      /* You don't have an exact match so look for close matches */
      SELECT @iRecCount = count(*) FROM Authors
       WHERE Zip LIKE substring(@cZip, 1, 4) + '%'
      IF @iRecCount > 0
        BEGIN
          --** You've got data that meets the modified criteria.
          --**   Return the closest match
          SET ROWCOUNT 1  
          SELECT Match='Close', Zip FROM Authors
           WHERE Zip LIKE substring(@cZip, 1, 4) + '%'
           ORDER BY Zip
          SET ROWCOUNT 0
        END
    END

  SET NOCOUNT OFF

You can add more code if you need close matches based on only 3 characters or less.


0
 

Author Comment

by:csindorf
ID: 1090456
Thanks for the help
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question