Hi
I have a database of records with zipcodes, and some of the zipcodes are only 4 digits long. I think this is because of the program I used, but I need to know if there is any way to modify the records in mysql.
To make a zipcode that looks like this: 8401 Look like this: 08401
Is this possible with a query? Do I have to use php in a loop?
Any info/samples would be super!
Thanks in advance.
Correcting Zipcodes In Mysql
Moderator: General Moderators
What kind of zip cpdes do you want to store? Do they all have 5 digits? (e.g. Australian zipcodes have 4 digits).
In mysql you can declare a numeric field with a specific length and the attribute ZEROFILL, see http://dev.mysql.com/doc/refman/4.1/en/ ... types.html
or you format the "number" with LPAD(str,len,padstr), see http://dev.mysql.com/doc/refman/5.1/en/ ... tions.html
In php you can use str_pad to get a string of a specific (min) length, see http://de2.php.net/strpad
You may also store the zip code in a text field
In mysql you can declare a numeric field with a specific length and the attribute ZEROFILL, see http://dev.mysql.com/doc/refman/4.1/en/ ... types.html
or you format the "number" with LPAD(str,len,padstr), see http://dev.mysql.com/doc/refman/5.1/en/ ... tions.html
In php you can use str_pad to get a string of a specific (min) length, see http://de2.php.net/strpad
You may also store the zip code in a text field
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You are going to have to either
a) Store the zip codes as varchar(5) ( or varchar(10) depending if you want zip+4) or
b) Manipulate the string code side so that it becomes 5 digits long.
If you are doing calculations on proximity, you are probably going to want to store them as varchar fields so that your selects work properly without a lot of DB side manipulation in your queries.
a) Store the zip codes as varchar(5) ( or varchar(10) depending if you want zip+4) or
b) Manipulate the string code side so that it becomes 5 digits long.
If you are doing calculations on proximity, you are probably going to want to store them as varchar fields so that your selects work properly without a lot of DB side manipulation in your queries.
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA