I want to store UK postal codes in the database. Is it OK to store those postal codes without the spaces?
-
Yea its safe. All UK postcodes end with 1 number and two letters. Then a space and whatever is left. London postcodes are good examples with various start codes but the last 3 are always `xyy` x-digit y-alpha no matter what. – Piotr Kula Jul 14 '14 at 13:07
7 Answers
Last 3 are always xyy
x
Digit 0-9yy
Alpha A-Z
Anything before is the first part of the grid reference and has various formats.

- 9,597
- 8
- 59
- 85
It's usually safe to remove the space. As others have said, you can re-insert the space later if required. The existence of a space between Outcode and Incode will not normally affect postal delivery. You should not have any non alpha numeric characters in a UK postcode, so if you see a dash you can safely remove it.
I work for Experian Data Quality and if your aim is clean data you may want to consider an address verification web service, like our Pro On Demand product. This will ensure you capture the correct postcode, as they can change over time, and that it is formatted correctly for your database.

- 3,361
- 1
- 21
- 19
It is okay to store without a space because you can always add an empty space back in to each postcode string - the heuristic is pretty simple.
As some other users have very helpfully explained, all UK postcodes have two groups of numbers and letters, separated by a space. The group following the space always contains a number and then two letters (thus, there are always three characters after the space). The group before the space will have either two, three, or four characters (see this Wikipedia page) and the screenshot below.
So, you can recreate the correct spacing by adding a space before the third-to-last character.
In R
, it looks like this (but the same logic would work in other languages, such as Python
):
#list of example postcodes
postcodes = c("LS176JA", "OX41EZ", "A99AA")
#add space to each postcode in the list of example postcodes
for (postcode in postcodes){
last_three = str_sub(postcode, start = -3)
first_x = str_replace(postcode, last_three, "")
final_postcode = paste0(first_x, " ", last_three)
print(final_postcode)
}
Which returns:
[1] "LS17 6JA"
[1] "OX4 1EZ"
[1] "A9 9AA"

- 657
- 8
- 16
we store postcodes and we accept inouts in any format, space or no space, but then strip or correct the entry for data storage
we find it works better this way when using the data for other things
Why would you want to store with no spaces?

- 45
- 1
- 7
Uk postcodes have a variety of formats: list of formats
Why are you unable to store white spaces?

- 13,132
- 18
- 79
- 148
-
1I can store the data with spaces. My concern is that users may be entering postal codes with or without spaces, maybe even dashes?. I want to make sure that the data is as clean as possible in the database. – marcin_koss Nov 07 '11 at 15:38
As others have said, there is no problem with removing all spaces and storing them, if that is what you want to do. As has been said, you can always format them with a space before the last three characters.
However, I would normally take them in any reasonable format, strip all spaces out, and them store them with this one extra space. The storage requirements are not an issue, and it makes it easier to simply display as it is. You would need to resolve the format before saving in some way, so you may as well save it as it is needed.

- 3,099
- 1
- 15
- 33
-
The other advantage, if you were ever doing any analysis or suchlike on this data (which may prove valuable) it is easy to divide into the two parts. You can identify where people are from by the first part of the postcode, and in this case, it is easier to do this with data already formatted. – Schroedingers Cat Nov 07 '11 at 15:28