← Back to SQL, page 9

sql-0173 · Easy fresher

Should postal codes and phone numbers be stored as integers or as text?

Store them as text. They are identifiers, not quantities: you never add them, they can have leading zeros ('02134'), and they often contain non-digits such as '+', spaces or letters (UK postcodes). An integer column silently drops leading zeros.

Full answer
sql
CREATE TABLE addr (zip_int INTEGER, zip_text TEXT);
INSERT INTO addr VALUES ('02134', '02134');
SELECT zip_int, zip_text FROM addr;
zip_intzip_text
213402134

The INTEGER column turned '02134' into 2134. The text column kept it. The same applies to account numbers, product codes and ids from other systems: if arithmetic on the value makes no sense, it is not a number.

Text also handles international formats such as 'SW1A 1AA' or '+44 20 7946 0018'. Validate the format with a CHECK or in the application, and normalize (for example to E.164 for phones) so that comparisons work.

sql
CREATE TABLE addr (zip_int INTEGER, zip_text TEXT);
INSERT INTO addr VALUES ('02134', '02134');
SELECT zip_int, zip_text FROM addr;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Identifiers that are never calculated belong in text
  • Integers drop leading zeros
  • Many codes contain letters or symbols
  • Validate and normalize text identifiers

Common mistakes

  • Storing ZIP codes as integers and losing leading zeros
  • Using numeric types for credit card or account numbers

Follow-ups

  • How would you validate a 5-digit postal code column?
  • Would you ever store an id from another system as an integer?

Tips

  • Ask: would you ever add two of these values?