Question

Consider the following:

CountryCode ZipCodeFrom ZipCodeTo Truck
-------------------------------------------
    UK      AA1234      AG4321    Truck 1
    UK      AG4322      AL9999    Truck 2
    UK      AM0000      AZ9999    Truck 3
    UK      BA0000      ZZ9999    Truck 4
    SE      0000        3333      Truck 10
    SE      3334        9000      Truck 11
    SE      9001        9999      Truck 12

Each line represents a area that one delivery truck is resposible for.

Say one Order was to be shipped to the postal code AB4000, Truck 1 should be used. Postal Code 5000 would be truck 11

Some countries use a mix of letters and numbers in their postal codes, some only use numbers.

Is there an easy way to find the corrosponding delivery truck (postal code range) given a postal code.

I have tried using between, but it doesnt seem to work for me. Maybe i need to create some rules for each country zipcode?

Can be solved in SQL or C#

Update:

select * from PostalCode where CountryCode = 'DK' and 'AB4000' between ZipCodeFrom and ZipCodeTo

select * from PostalCode where CountryCode = 'DK' and 'AB4000' <= ZipCodeTo and 'AB4000' >= ZipCodeFrom

This is what i have tried

I will try the suggestion Wietze314 came up with.

Was it helpful?

Solution

I think in order to do a BETWEEN, you could convert the letters to numbers (01 to 26) So

UK AA1234 AG4321 Truck 1

Becomes

UK 01011234 01074321 Truck 1

Beware of the zeroes! they need to be there.

OTHER TIPS

Normally i would create a function within the sql-server. Because you don't want to retrieve all rows and compare them local. It should do something like a between but stripping the alphabetics first. The function prevents implementing code more than twice.

It depends how good your areas need to be with regards actual geographical proximity.

see another user's question - Zipcode based search

and also, as another example here's a map of UK post code areas http://www.freemaptools.com/uk-postcode-map.htm

my point is basically that selecting between two sequential numbers will only be good up to a point, like a major geographical identifier like a county, and the rules will likely change from country to country.

I created a test with the table and data below:

CREATE TABLE [PostalCodes](
    [CountryCode] [nvarchar](50) NULL,
    [ZipCodeFrom] [nvarchar](50) NULL,
    [ZipCodeTo] [nvarchar](50) NULL,
    [TruckId] [nvarchar](50) NULL
) 

insert into [PostalCodes]
values('UK','AA1234','AG4321','Truck 1');

insert into [PostalCodes]
values('UK','AG4322','AL9999','Truck 2');

insert into [PostalCodes]
values('UK','AM0000','AZ9999','Truck 3');

insert into [PostalCodes]
values('UK','BA0000','ZZ9999','Truck 4');

insert into [PostalCodes]
values('SE','0000','3333','Truck 10');

insert into [PostalCodes]
values('SE','3334','9000','Truck 11');

insert into [PostalCodes]
values('SE','9001','9999','Truck 12');

Running this select i get the expected result:

select * 
  from [PostalCodes] 
 where CountryCode ='SE' 
   and '0200' between ZipCodeFrom and ZipCodeTo

Gives Truck 10

Replace '0200', with '10000' you get Truck 10 since '10000' is between '0000' and '3333' according to the alphabeth. Might not be what you expected though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top