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 7 1942
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: - WHILE (SELECT REP FROM REPS WHERE REPZIP = @SEARCHEDZIP)
-
-
BEGIN
-
-
IF (SELECT COUNT(REP) FROM REPS WHERE REPZIP = @SEARCHEDZIP) > 0
-
--we have the rep
-
BREAK
-
-
ELSE
-
--increment the zip
-
SET @SEARCHEDZIP = @SEARCHEDZIP + 1
-
CONTINUE
-
-
END
It;s a long shot but maybe gets you closer?
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: -
select top 1 * from YourZipCodeTable
-
where zipcode >= @YourZipCode
-
order by zipcode
-
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
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 0060 9, then DOWN to 0060 7, then try 006 10, then 0060 6, 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: -
select top 1 * from YourZipCodeTable
-
where zipcode >= @YourZipCode
-
order by zipcode
-
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
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: -
WHILE (SELECT REP FROM REPS WHERE REPZIP = @SEARCHEDZIP OR REPZIP = @SEARCHEDZIP2)
-
BEGIN
-
IF (SELECT COUNT(REP) FROM REPS WHERE REPZIP = @SEARCHEDZIP) > 0 || (SELECT COUNT(REP) FROM REPS WHERE REPZIP = @SEARCHEDZIP2) > 0
-
--we have the rep
-
BREAK
-
ELSE
-
--increment the zip
-
SET @SEARCHEDZIP = @SEARCHEDZIP + 1
-
SET @SEARCHEDZIP2 = @SEARCHEDZIP2 - 1
-
CONTINUE
-
-
CONTINUE
-
-
END
-
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: - WHILE (SELECT REP FROM REPS WHERE REPZIP = @SEARCHEDZIP)
-
-
BEGIN
-
-
IF (SELECT COUNT(REP) FROM REPS WHERE REPZIP = @SEARCHEDZIP) > 0
-
--we have the rep
-
BREAK
-
-
ELSE
-
--increment the zip
-
SET @SEARCHEDZIP = @SEARCHEDZIP + 1
-
CONTINUE
-
-
END
It;s a long shot but maybe gets you closer?
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. - declare @myzip char(5)
-
-
set @myzip = '00608'
-
-
-
select @myzip, zipcode, cast(zipcode as int)-cast(@myzip as int) as distance
-
from myZipCodeTable where cast(@myzip as smallint) between cast(zipcode as int) - 10 and cast(zipcode as int) + 10
-
order by abs(cast(zipcode as int)-cast(@myzip as int)), zipcode desc
Happy coding.
-- CK
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. - declare @myzip char(5)
-
-
set @myzip = '00608'
-
-
-
select @myzip, zipcode, cast(zipcode as int)-cast(@myzip as int) as distance
-
from myZipCodeTable where cast(@myzip as smallint) between cast(zipcode as int) - 10 and cast(zipcode as int) + 10
-
order by abs(cast(zipcode as int)-cast(@myzip as int)), zipcode desc
Happy coding.
-- CK
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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,...
|
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...
|
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>...
| |
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |