The postcodes that lost a zero

A youth association sends a kit by courier to each participant in its programming contest. The registration website produces a CSV with the addresses, which is uploaded to the courier’s platform.

This year, before uploading it, a volunteer double-clicked it to open it in Excel, corrected a couple of names and saved it.

The platform rejected dozens of parcels with “invalid postcode”, all going to Álava, Albacete, Alicante, Almería, Ávila, Badajoz, the Balearic Islands, Barcelona or Burgos. Those to Madrid, Seville or Valencia went through, and on the website every postcode was correct.

Why did only the parcels to those provinces fail?

How to solve the cases
Spreadsheet with a column of codes next to several parcels with shipping labels

Hints

Reveal them one at a time, only when you need them.

Solution

When the CSV was opened, Excel treated the postcodes as numbers and removed the leading zero. When it was saved, “01001” became “1001”, and the platform rejected the four-digit codes. Only provinces whose codes start with 0 were affected.

What happened?

A CSV is a text file: it does not say whether “01001” is a number or text. When it opens the file, Excel decides the type of each value and, if it looks like a number, converts it. In a number, leading zeros have no value, so they disappear. Microsoft documents this: Excel automatically removes leading zeros, including when it opens a .csv file.

Spanish postcodes have five digits and, under the Order of 23 January 1984, the first two identify the province using the national geographic code. In that coding, published by the INE (Spain’s National Statistics Institute), codes 01 to 09 are Álava, Albacete, Alicante, Almería, Ávila, Badajoz, the Balearic Islands, Barcelona and Burgos. This is what happened:

  1. The website exported a correct CSV, with codes such as 01001 or 08001.
  2. When it was opened with a double-click, Excel converted that column into numbers: 01001 became 1001, and 08001, 8001.
  3. The volunteer saved the file as CSV. Excel wrote the values as it held them, without the zero.
  4. The courier platform expected five digits and rejected the four-digit codes. Those for Madrid (28…), Seville (41…) or Valencia (46…) do not start with zero and did not change.

That is why the problem seemed to depend on the province: it only affected the nine whose codes start with 0.

How could it be fixed and prevented?

To fix it, the CSV needed to be exported again from the website instead of trying to repair the modified one. If anything needed correcting, it could be done in a text editor or by importing the file into Excel with Data > From Text/CSV, opening the Power Query editor and changing the postcode column type to Text under Data Type before loading it.

To stop it from happening again, several measures helped:

  • Do not double-click CSV files that contain codes. It is better to import them and specify that postcode, phone number or ID number columns are text.
  • Turn off automatic conversion in Excel. In versions that include it, such as Microsoft 365 and Excel 2024, go to File > Options > Data > Automatic Data Conversion and clear “Remove leading zeros from numerical text and convert to a number”.
  • Validate the data before sending it, for example by checking that every postcode has five digits.
  • Treat identifiers as text when they are not used for calculations, even if they only contain digits.

Further reading

Why Excel removes leading zeros and how to import a column as text is explained in Keeping leading zeros and large numbers — Microsoft Support.

To turn off automatic conversion, including when opening .csv files, see Set automatic data conversions — Microsoft Support.

The structure of Spanish postcodes is set out in the Orden de 23 de enero de 1984 por la que se desarrolla el código postal — BOE (in Spanish).

Each province’s code is listed in the INE’s Relación de provincias con sus códigos — INE (in Spanish).

Have a story worth investigating?

Suggest a case

A project by Eniun

How do you solve the cases?

Each case presents a mystery that seems to defy explanation.

A moderator who knows the solution reads the mystery to the group. The group works out what happened by asking questions, discussing hypotheses and using what they know.

You can project the case on a screen so everyone can read it and refer back to it during the discussion.

The moderator can guide the discussion without giving away the answer. If the group gets stuck, each case offers three graded hints under “Hints”, which can be revealed one at a time.

Once the moderator confirms that the group has found the answer, select “Show answer” to open the solution.

You can also solve the cases on your own: read the story, try to come up with an explanation and use the hints only when you need them.