473,503 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Always return something on a ZipCode Search

1 New Member
Hi, I've got a table of Sale Reps (ID, Name, Address, City, State, Zipcode, etct). I'm trying to create a stored proc that will accept a ZipCode and search for Reps with that ZipCode. If there isn't an exact match I want the query to expand in increments of 1, up and down, until it finds a rep in the closest ZipCode.

I don't do much SQL, but I have a feeling this is a straightforward query, I'm just not seeing it.

Thanks so much.

-Michael
http://www.radiantdays.com
Feb 12 '08 #1
7 1942
abev
22 New Member
Hi, I've got a table of Sale Reps (ID, Name, Address, City, State, Zipcode, etct). I'm trying to create a stored proc that will accept a ZipCode and search for Reps with that ZipCode. If there isn't an exact match I want the query to expand in increments of 1, up and down, until it finds a rep in the closest ZipCode.

I don't do much SQL, but I have a feeling this is a straightforward query, I'm just not seeing it.

Thanks so much.

-Michael
http://www.radiantdays.com

michael I dont have a direct answer but lets brainstorm...

psedo tsql code:

Expand|Select|Wrap|Line Numbers
  1. WHILE (SELECT REP FROM REPS WHERE REPZIP = @SEARCHEDZIP) 
  2.  
  3. BEGIN
  4.  
  5.     IF (SELECT COUNT(REP) FROM REPS WHERE REPZIP = @SEARCHEDZIP) > 0
  6.        --we have the rep
  7.         BREAK
  8.  
  9.     ELSE
  10.    --increment the zip
  11.     SET @SEARCHEDZIP = @SEARCHEDZIP + 1
  12.         CONTINUE
  13.  
  14. END
It;s a long shot but maybe gets you closer?
Feb 13 '08 #2
ck9663
2,878 Recognized Expert Specialist
Hi, I've got a table of Sale Reps (ID, Name, Address, City, State, Zipcode, etct). I'm trying to create a stored proc that will accept a ZipCode and search for Reps with that ZipCode. If there isn't an exact match I want the query to expand in increments of 1, up and down, until it finds a rep in the closest ZipCode.

I don't do much SQL, but I have a feeling this is a straightforward query, I'm just not seeing it.

Thanks so much.

-Michael
http://www.radiantdays.com

Let's see if I understand you right. You have a zip code(ie. zip = '00607'). You'll search the zip code table. If it's existing, it will return the row, and of course all the columns in it. If it's not, you'll search '00608'. Since the next valid zip code is '00610', you will have to increment the zip code value, until it reach '00610', return that record and get the other columns that you need.

If I am wrong, please don't continue reading. If am right, you can create a simple stored proc with this query inside:

Expand|Select|Wrap|Line Numbers
  1. select top 1 *  from YourZipCodeTable
  2. where zipcode >= @YourZipCode
  3. order by zipcode
  4.  
Your objective is to get the first record that exactly match the zip code you're looking for or @yourzipcode+1..N = ZipCodeOnTheTable. Which means if not existing, it will always return the first ZipCodeOnTheTable that's greater than @yourzipcode.

I hope I make sense.

Good luck.

-- CK
Feb 13 '08 #3
mfitzgerald1
4 New Member
CK,
Thanks so much. Yes, you're understanding me and this makes sense.I think your answer gets me halfway there. But they want the search to expand both up and down. Meaning if we start with 00608 but that returns nothing, they want to go to 00609, then DOWN to 00607, then try 00610, then 00606, so it's an ever-expanding set away from the original.

Our VPN is down, but I'll fiddle with this when I get in in the morning.

Thanks again.
-m



Let's see if I understand you right. You have a zip code(ie. zip = '00607'). You'll search the zip code table. If it's existing, it will return the row, and of course all the columns in it. If it's not, you'll search '00608'. Since the next valid zip code is '00610', you will have to increment the zip code value, until it reach '00610', return that record and get the other columns that you need.

If I am wrong, please don't continue reading. If am right, you can create a simple stored proc with this query inside:

Expand|Select|Wrap|Line Numbers
  1. select top 1 *  from YourZipCodeTable
  2. where zipcode >= @YourZipCode
  3. order by zipcode
  4.  
Your objective is to get the first record that exactly match the zip code you're looking for or @yourzipcode+1..N = ZipCodeOnTheTable. Which means if not existing, it will always return the first ZipCodeOnTheTable that's greater than @yourzipcode.

I hope I make sense.

Good luck.

-- CK
Feb 13 '08 #4
mfitzgerald1
4 New Member
Abev,
Thanks so much. I'll try it. It looks like it might work, but since they want the search to go both up and down, maybe I'll need to set a second searchzip. Something like this:
Expand|Select|Wrap|Line Numbers
  1.        WHILE (SELECT REP FROM REPS WHERE REPZIP = @SEARCHEDZIP OR REPZIP = @SEARCHEDZIP2)
  2.        BEGIN
  3.            IF (SELECT COUNT(REP) FROM REPS WHERE REPZIP = @SEARCHEDZIP) > 0 || (SELECT COUNT(REP) FROM REPS WHERE REPZIP = @SEARCHEDZIP2) > 0
  4.               --we have the rep
  5.                BREAK
  6.            ELSE
  7.           --increment the zip
  8.            SET @SEARCHEDZIP = @SEARCHEDZIP + 1
  9.            SET @SEARCHEDZIP2 = @SEARCHEDZIP2 - 1
  10.                CONTINUE
  11.  
  12.         CONTINUE
  13.  
  14.        END
  15.  
I'll try it when I get in in the morning (VPN went down) and let you know. Thanks again.
-m

michael I dont have a direct answer but lets brainstorm...

psedo tsql code:

Expand|Select|Wrap|Line Numbers
  1. WHILE (SELECT REP FROM REPS WHERE REPZIP = @SEARCHEDZIP) 
  2.  
  3. BEGIN
  4.  
  5.     IF (SELECT COUNT(REP) FROM REPS WHERE REPZIP = @SEARCHEDZIP) > 0
  6.        --we have the rep
  7.         BREAK
  8.  
  9.     ELSE
  10.    --increment the zip
  11.     SET @SEARCHEDZIP = @SEARCHEDZIP + 1
  12.         CONTINUE
  13.  
  14. END
It;s a long shot but maybe gets you closer?
Feb 13 '08 #5
ck9663
2,878 Recognized Expert Specialist
Try running this on your query analyzer and play around with the value of @myzip variable if it keeps returning the right one. You have to include TOP 1 when you're satisfied with the result. I just included the zipcode and distance column so that you can further analyze the result.

One last catch: It will only look at the zipcode +/- 10. If you need to extend the range, you just have to increase it.

Expand|Select|Wrap|Line Numbers
  1. declare @myzip char(5)
  2.  
  3. set @myzip = '00608'
  4.  
  5.  
  6. select @myzip, zipcode, cast(zipcode as int)-cast(@myzip as int) as distance
  7. from myZipCodeTable where cast(@myzip as smallint) between cast(zipcode as int) - 10 and cast(zipcode as int) + 10
  8. order by abs(cast(zipcode as int)-cast(@myzip as int)), zipcode desc
Happy coding.

-- CK
Feb 13 '08 #6
mfitzgerald1
4 New Member
CK,
That's very cool. Thanks.
-m

Try running this on your query analyzer and play around with the value of @myzip variable if it keeps returning the right one. You have to include TOP 1 when you're satisfied with the result. I just included the zipcode and distance column so that you can further analyze the result.

One last catch: It will only look at the zipcode +/- 10. If you need to extend the range, you just have to increase it.

Expand|Select|Wrap|Line Numbers
  1. declare @myzip char(5)
  2.  
  3. set @myzip = '00608'
  4.  
  5.  
  6. select @myzip, zipcode, cast(zipcode as int)-cast(@myzip as int) as distance
  7. from myZipCodeTable where cast(@myzip as smallint) between cast(zipcode as int) - 10 and cast(zipcode as int) + 10
  8. order by abs(cast(zipcode as int)-cast(@myzip as int)), zipcode desc
Happy coding.

-- CK
Feb 13 '08 #7
mfitzgerald1
4 New Member
CK,
Thanks again. This worked perfect.

And thank Abev. I futz with the While Loop for... well...a while and couldn't get it. But I'm sure it would have eventually.
I appreciate both your time.

-michael
Feb 13 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
4799
by: Adrian Parker | last post by:
Hi. I would like to query a database, given several where clauses to refine my search, and return the value of one single field in the database. eg: I have a table that lists teachers. Their...
3
5868
by: Eric the half a Bee | last post by:
Hello I am trying to implement a search function within a collection of Employees. I am searching for a specific EmpID number in the collection, and if it is found, I want to return the...
8
1535
by: JSheble | last post by:
I'm writing some code that interacts with MSMQ, and am getting in the habit of using try{}catch{}finally{} blocks, but am now running into a problem with un-initialized variables... for example,...
2
5110
by: premgrps | last post by:
Hi, I have a database with two tables a) A table of 2 million records with city, zip and associated information (say XYZ) and b) zipcode latitude, longitude table having >40,000 records/zip...
1
2607
by: jhoff | last post by:
I'm trying to use javascript to execute code when the enter key is pressed in a text box. Basically, I'm doing this... <html> <body onload="load()" onunload="GUnload()"> <form name=zipform>...
17
4565
by: pbd22 | last post by:
Hi. How does one return a range of rows. I know that "Top 5" will return rows 0 - 5 but, how do I get 6 - 10? thanks
0
1232
by: Tina | last post by:
I need to be able to provide a proximity search by zip code. I have a table of job opportunities with the zip code where they exist. So I need to be able to list zip codes that are within xx...
6
12709
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.