Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-07-21 : 09:39:49
|
| Bhavin writes "Hi,I am working on a project where I have to write store procedure to return five nearest Postalcode from the user selected Postal code in Canada.In Canada postalCodes are varchar and it increments alphanumerically with the distance. I will really appreciate if you can guide me for this. It is like MApquest search for five nearest location of selected PostalCode.Regards,Bhavin Shah." |
|
|
Q
Yak Posting Veteran
76 Posts |
Posted - 2006-07-21 : 10:00:30
|
| What should happen when there are over 5 identical postalcodes which are nearest to you own postalcode? --still thinking about a solution... |
 |
|
|
Q
Yak Posting Veteran
76 Posts |
Posted - 2006-07-21 : 10:21:48
|
| Option:convert the postalcode into a numeric value: cast(cast(ascii('a') as varchar)+cast(ascii('b')as varchar)+cast(ascii('c')as varchar)+cast(ascii('1')as varchar) as decimal)--instead of using the defined chars you should use each char from the postalscalculate the absolute difference between your own postalcode-value and the surrounded postalcodes postalcodes with the smallest difference will be the nearest so order by and take the top 5.. Hopefully it´s clear, because this description isn´t... |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-21 : 11:56:42
|
quote: convert the postalcode into a numeric value
That seems sensible to me. The NTODEC function below can help with that...--datadeclare @t table (postcode varchar(7))insert @t select 'K1A 0B1'union all select 'H0H 0H0'union all select 'K0H 1T3'union all select 'H4H 5T5'--calculationselect postcode, dbo.NTODEC(replace(postcode, ' ', ''), 36) from @t/*resultspostcode -------- ----------- K1A 0B1 1211470093H0H 0H0 1028718756K0H 1T3 1210119015H4H 5T5 1035444137/*--NTODEC functionGOCREATE function NTODEC(@s VARCHAR(255), @b int) returns int asBEGIN DECLARE @i int, @temp char(1), @result int SET @i = 1 SET @result = 0 WHILE (@i<=LEN(@s)) BEGIN SET @temp = UPPER(SUBSTRING(@s, @i, 1)) IF (@temp >= '0') AND (@temp <= '9') SELECT @result = @result + (ASCII(@temp)-48) * POWER(@b, LEN(@s)-@i) ELSE SELECT @result = @result + (ASCII(@temp)-55) * POWER(@b, LEN(@s)-@i) SET @i = @i+1 END return @resultEND The NTODEC function is taken from here:http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=502&lngWId=5Anyone interested in Canadian postal codes can look here:http://en.wikipedia.org/wiki/Canadian_postal_codeRyan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
gugarc
Starting Member
17 Posts |
Posted - 2006-07-21 : 14:12:32
|
| when you say the five nearest, you mean, two postalcodes above, the postalcode itself and two codes bellow ---> this query works (using subqueries isn´t very fast - poor performance) - but it may help:DECLARE @postalcode varchar(7)SET @postalcode = 'xxxxxxx' --the code to be searched forselect * from @t where postcode in (select top 3 postcode from @t where postcode >= @postalcode order by postcode)or postcode in (select top 2 postcode from @t where postcode < @postalCode order by postcode desc)order by postcode(you can use a OR combination instead of a UNION query - OR = '+' ) |
 |
|
|
|
|
|
|
|