Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 897791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:56:19+00:00 2026-05-15T14:56:19+00:00

In my MySQL InnoDB database, I have dirty zip code data that I want

  • 0

In my MySQL InnoDB database, I have dirty zip code data that I want to clean up.

The clean zip code data is when I have all 5 digits for a zip code (e.g. “90210”).

But for some reason, I noticed in my database that for zipcodes that start with a “0”, the 0 has been dropped.

So “Holtsville, New York” with zipcode “00544” is stored in my database as “544“

and

“Dedham, MA” with zipcode “02026” is stored in my database as “2026“.

What SQL can I run to front pad “0” to any zipcode that is not 5 digits in length? Meaning, if the zipcode is 3 digits in length, front pad “00”. If the zipcode is 4 digits in length, front pad just “0”.

UPDATE:

I just changed the zipcode to be datatype VARCHAR(5)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:56:19+00:00Added an answer on May 15, 2026 at 2:56 pm

    Store your zipcodes as CHAR(5) instead of a numeric type, or have your application pad it with zeroes when you load it from the DB. A way to do it with PHP using sprintf():

    echo sprintf("%05d", 205); // prints 00205
    echo sprintf("%05d", 1492); // prints 01492
    

    Or you could have MySQL pad it for you with LPAD():

    SELECT LPAD(zip, 5, '0') as zipcode FROM table;
    

    Here’s a way to update and pad all rows:

    ALTER TABLE `table` CHANGE `zip` `zip` CHAR(5); #changes type
    UPDATE table SET `zip`=LPAD(`zip`, 5, '0'); #pads everything
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.