Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4060846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:23:27+00:00 2026-05-20T15:23:27+00:00

I have the simplest table in the world, for looking up lat/lng values for

  • 0

I have the simplest table in the world, for looking up lat/lng values for a UK postcode (loaded with full UK postcode data):

CREATE TABLE postcodes (
  postcode char(7) NOT NULL,
  lat double(10,6) NOT NULL,
  lng double(10,6) NOT NULL,
  KEY postcode (postcode)
)

Postcodes in ‘postcode’ field either have 2 digits at the end of the first half, or one and then a space. I think the space is important for the integrity of how they are matched (??), and besides I don’t want to remove the spaces in the table, as I’m also pulling out the postcodes for display purposes (and I don’t want a duplicate field, because I’m fussy!). Examples:

'LE115AF', 'BS6 5EE', 'W1A 1AA', 'BS216RS', 'M3 1NH'

So, some have spaces, some don’t. Most are 7 chars overall, some only 6.

Anyway, point being is I want users to be able to enter postcode queries, including partial postcodes, with or without spaces, and always find a match if their input string is valid (i.e. they don’t enter a full or partial postcode which doesn’t exist in the table).

This is how I’ve done it so far (with some help from PHP):

{...} WHERE `postcode` LIKE '" . str_replace(' ','%',$query) . "%' LIMIT 1

This is good for:

  • full postcodes which don’t contain spaces in the db
  • partial postcodes if a space has been entered and there is a corresponding space in the db, or the queried portion stops short of where the space occurs (e.g. ‘W1A’ will match ‘W1A 1AA’, ‘M3 1’ will match ‘M3 1AR’, etc).

But doesn’t work for these queries:

  • ‘W1A1AA’ should match ‘W1A 1AA’
  • ‘BS65EE’ should match ‘BS6 5EE’
  • ‘BS65’ should match the first ‘BS6 5%’ postcode in db, which is ‘BS6 5AA’
  • ‘M31’ should likewise match ‘M3 1AR’

I’m guessing I need to somehow do some MySQL string function magic to work out if there’s a space in the row’s postcode field, and adjust my WHERE clause logic accordingly? Anyone got any advice on the best approach? I ideally want to also:

  • avoid MySQL stored procedures (inline functions preferred)
  • do nothing more than inline string functions in the PHP part too
  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:23:28+00:00Added an answer on May 20, 2026 at 3:23 pm

    Create a new column which is just the postcode field with the spaces stripped, and create a unique index on it. You shouldn’t find any duplicates. That should reassure you that the space really isn’t important 🙂

    Then use that for the lookup, after stripping spaces on your input postcodes.

    Bear in mind that solutions that involve applying string functions to the postcode column of the table may stop MySQL from using any indexes on that column. (The index is based on the exact data in the column, so if you start applying functions to that data, the optimiser will generally decide that the index is useless.)

    If you do feel the need to reformat things, the easiest option is to work from the knowledge that while the “outbound” part of the postcode — the part before the space — varies in format a little, the “inbound” part — the part after the space — is always a single digit followed by two letters.

    Probably the best resource on the format I’ve ever found is the Wikipedia entry, by the way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to create the simplest WinAPI window using assembly NASM. I have problem
I have the following problem: my table is big enought (millions of data rows),
I have xml file with data that i have to display in table. Now
I have the simplest PhoneGap app one could imagine! All I'm trying to do
I have the simplest database entry that when I hit submit, it takes me
I have the simplest code that I want to separate in three files: Header
I have a very simple question. Trying to figure out what is the simplest
I have no idea how to describe my problem. It is the simplest way
What's the simplest way to code against a property in C# when I have
I have the simplest problem to implement, but so far I have not been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.