0

I have Mysql table which include zip code and it is int.

The problem is when I insert a zip code begin with 0 " 0123" it saves like "123" without zero.

How to solve it in mysql structure ?

regards

Ilona
  • 357
  • 3
  • 10
Marvos
  • 15
  • 8

2 Answers2

5

You could change your zip_code field data type to something like varchar:

ALTER TABLE table_name MODIFY column_name VARCHAR(6);

Or if you prefer to keep it as an integer and fill it with leading zeros you can use ZEROFILL:

ALTER TABLE table_name MODIFY column_name INT(6) ZEROFILL;

e.g. 123 becomes 000123, 1234 becomes 001234 etc.. (depends on length)

Ben M.
  • 311
  • 1
  • 9
3

Don't store the zip codes as int.

Storing the zip-codes as varchar(10) or something like that will keep your zero padding and also cater for alphanumeric zip codes which may be used in some areas.

Cobus Kruger
  • 8,338
  • 3
  • 61
  • 106