Avatar of ecperi58
ecperi58 asked on

Entering a number string with 0 in front in MySQL DB

Hi,

I am trying to import a csv file of zip codes into a MySQL database. The zip codes that start with 0 are being entered without the 0's in front. Can I remedy this?

Thanks
MySQL Server

Avatar of undefined
Last Comment
ecperi58

8/22/2022 - Mon
Terry Woods

This exact problem seems to have been asked before!
https://www.experts-exchange.com/Database/MySQL/Q_22909269.html
Terry Woods

The solution I linked to above assumes your zip code column is an integer type. Alternatively, you can change the column to be a varchar - there's an example of this too:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_20471872.html

Let me know if you have any issues
ASKER
ecperi58

My table has 2 columns, one called school_id and one called zip. If I do the ZEROFILL will it add a zero to both fields? I would just like the 0 added to the zip codes on the zip column that did not import the beginning 0 (sometimes 2 beginning zeros).
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Terry Woods

The zerofill option just applies to an individual column, however it will apply to all rows in the table, up to the number of characters indicated by the column length eg int(5) would add zeroes to make it up to 5 characters. Will this meet your requirements?
ASKER
ecperi58

How would I indicate which column I wanted this applied to if the table name was zip_achool and the column was zip?

Thanks
ASKER CERTIFIED SOLUTION
Terry Woods

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
ecperi58

Thanks so much for the prompt reply!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.