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
Solved

search

Posted on 1998-10-08
2
226 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
2 Comments
 
LVL 1

Accepted Solution

by:
gertrude earned 60 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

856 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