Maik Lowrey
Maik Lowrey

Reputation: 17586

Which column format for german ZIP codes with a leading 0?

I would like to save postcodes in my database. There are postcodes that begin with a 0, and not only in Germany. Unfortunately, the integer format would not work because the 0 would be omitted. So 01234 would then become 1234. Is varChar(5) the only possibility?

The German ZIP Code is always 5 digits.

Upvotes: 0

Views: 215

Answers (3)

If they're always going to be 5 characters, then use a datatype that's exactly 5 characters, i.e. char(5). You could even add a column constraint to ensure that anything inserted into the table is exactly 5 characters long and every char is a digit.

Upvotes: 1

BenM
BenM

Reputation: 529

Varchar(5) certainly isn't the only possibility. You could save them as integers, then pad them with leading zeroes in whatever application is using the database.

But IMO, saving them as strings (whether varchar or char) is the best option. Even though they're comprised of digits, they're not really numbers (e.g. it doesn't make sense to add them together, and leading zeroes are important). Saving them as strings would also give you flexibility if you do eventually need to use postcodes with letters in them.

Upvotes: 1

AnoE
AnoE

Reputation: 8345

Well, alphanumeric (anything with CHAR in the data type name...). Same as for phone numbers or other data that does primarily contain digits, but not only.

Upvotes: 1

Related Questions