
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Add leading zeros to a MySQL column?
To add leading zeros, you can use LPAD(). Let us first create a table −
mysql> create table DemoTable ( Code varchar(100) ); Query OK, 0 rows affected (0.87 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('JS'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('CB'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('DM'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('CT'); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+ | Code | +------+ | JS | | CB | | DM | | CT | +------+ 4 rows in set (0.00 sec)
Now, let us add leading zeros in MySQL −
mysql> update DemoTable set Code=LPAD(Code, 8, '0'); Query OK, 4 rows affected (0.10 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+----------+ | Code | +----------+ | 000000JS | | 000000CB | | 000000DM | | 000000CT | +----------+ 4 rows in set (0.00 sec)
- Related Articles
- Add leading Zeros to Python string
- Java Program to add leading zeros to a number
- Add leading Zeros to Python Program string
- How to Add Leading Zeros to a Number in Python?
- How to add a leading zero to some values in a column in MySQL?
- How to Add or Pad Leading Zeros to Numbers or Text in Excel?
- Java Program to Remove leading zeros
- Golang program to remove leading zeros
- Create an integer column in an R data frame with leading zeros
- How to Add Trailing Zeros to a Column of Numbers in Excel?
- Remove leading zeros in a JavaScript array?
- How to remove leading zeros from a number in JavaScript?
- How to pad a number with leading zeros in JavaScript?
- Remove Leading Zeros from a String in C#
- Remove leading zeros in array - JavaScript

Advertisements