?
Solved

search

Posted on 1998-10-08
2
Medium Priority
?
243 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

765 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