Solved

search

Posted on 1998-10-08
2
211 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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 setup several different housekeeping processes for a SQL Server.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now