2

In a logistics software which is written in C#, I need to check if given postal code is between one of the ranges at database.

For Germany, for example

Range: 47000-48000 Given Postal Code: 47057 Result: True

for numeric postal codes, it's alright. But what about UK postal codes? W11 2BQ is an example postal code from london.

one of the basic ideas is, converting postal codes to numbers by converting each character into its ascii code and writing left to right simply.

so

W11 2BQ -> 87 49 49 32 50 66 81 -> 87,494,932,506,681

so one simple postal code becomes a very big number and that disturbs me. english postal codes can vary in sizes (up to 8 chars) so this makes the resulting number even bigger.

I use sql server to check if given postal code is in range.

Is there any official technique to deal with UK postal codes for range calculation?

Best, Alper

H H
  • 263,252
  • 30
  • 330
  • 514
midwinter
  • 21
  • 5
  • 1
    You're going to have to find out what "between" means for ranges in your database (are those ranges defined by someone else?), and implement that. – Greg Hewgill Apr 06 '12 at 20:16

3 Answers3

0

I don't think converting the zip code into ASCII is a good idea. The reason is quite obvious. If you have a ASCII converted value like 1210121 (just an example), the problem is to separate as (12) (10) (121) or (12) (101) (21). This seems to be a lot of work for such little gain.

Although, couldn't you use SQL??

 select * from ZIPTable where zipcode IN ('G4543','G3543')

Note: You can get the zipcode values from a subquery.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
0

'Postal codes' are a completely arbitrary system, instead of attempting to code against it - unless your objective is to write a comprehensive postal code comprehension library - I would strongly recommend finding/paying for/stealing a library that can do what you're asking for.

If it helps, the basic rules for UK postal codes are (as far as I remember from ones I've seen):

[A-Z]{1,2} - 2-character code representing the sorting depot in the locality \d - subdivision of the sorting depot's jurisdiction [mandatory space] \d[A-Z]{2} - alphanumeric code for a contiguous region occupied by a group of 10-100 addresses

It wouldn't surprise me if my summarisation is wrong/incomplete. All the postal codes I've seen are in the form I mention, but I don't know the actual rules, so there could be others in a different format. It's included merely to give a broad appraisal of the nature of the system.

Tom W
  • 5,108
  • 4
  • 30
  • 52
0

The GeoNames Web Service might be a good place to start. It should be possible to validate postal codes through their API somehow. I think you can export the database too, if you want to write up your own validation logic.

James Johnson
  • 45,496
  • 8
  • 73
  • 110