- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to front pad zip code with “0” in MySQL?
To front pad zip code with 0, use LPAD() function in MySQL. The syntax is as follows −
SELECT LPAD(yourColumnName, columnWidth+1, '0') as anyVariableName from yourTableName;
To understand the above concept of LPAD() to add front pad zip code with 0, let us create a table. One of the columns of the table is Zip Code. The following is the query to create a table.
mysql> create table ZipCodePadWithZeroDemo −> ( −> Name varchar(200), −> YourZipCode int(6) −> ); Query OK, 0 rows affected (0.44 sec)
Insert some records in the table. The query to insert records is as follows −
mysql> insert into ZipCodePadWithZeroDemo values('John',23455); Query OK, 1 row affected (0.13 sec) mysql> insert into ZipCodePadWithZeroDemo values('Carol',46523); Query OK, 1 row affected (0.62 sec) mysql> insert into ZipCodePadWithZeroDemo values('Johnson',12345); Query OK, 1 row affected (0.11 sec) mysql> insert into ZipCodePadWithZeroDemo values('David',34567); Query OK, 1 row affected (0.18 sec)
Display all records from the table. The query is as follows −
mysql> select *from ZipCodePadWithZeroDemo;
The following is the output −
+---------+-------------+ | Name | YourZipCode | +---------+-------------+ | John | 23455 | | Carol | 46523 | | Johnson | 12345 | | David | 34567 | +---------+-------------+ 4 rows in set (0.00 sec)
Implement the syntax which we discussed in the beginning to add front pad zip code with value 0. The query is as follows −
mysql> SELECT LPAD(YourZipCode, 6, '0') as UPDATEDZIPCODE from ZipCodePadWithZeroDemo;
We will also get the name here −
mysql> SELECT Name,LPAD(YourZipCode, 6, '0') as UPDATEDZIPCODE from ZipCodePadWithZeroDemo;
The following output displays the Name as well as the Zip Code −
+---------+----------------+ | Name | UPDATEDZIPCODE | +---------+----------------+ | John | 023455 | | Carol | 046523 | | Johnson | 012345 | | David | 034567 | +---------+----------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to remove -XXX from Zip Code field using MySQL REGEXP?
- Validate the ZIP code with Java Regular expressions
- In MySQL, how can we pad a string with another string?
- Zip Code validation using Java Regular Expressions
- Pad the values of the column with zeros in MySQL
- How to update multiple rows and left pad values in MySQL?
- How to pad a number with leading zeros in JavaScript?
- How do you fill in or pad a column with zeros using a MySQL query?
- How to pad the input tensor boundaries with zero in PyTorch?
- How to set country code to column values with phone numbers in MySQL?
- How to add comments in MySQL Code?
- Working with zip files in Python
- How to pad the input tensor boundaries with a constant value in PyTorch?
- How to Zip a directory in PHP?
- Query MySQL with unicode char code?

Advertisements