Solved

search

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 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

18 Experts available now in Live!

Get 1:1 Help Now