Padding the beginning of a MySQL INT field with zeroes?


You can use LPAD() from MySQL to pad the beginning of a MySQL INT field with zeroes. Let us first create a table.

mysql> create table paddingDemo
   -> (
   -> Number int
   -> );
Query OK, 0 rows affected (0.49 sec)

Following is the query to insert some records in the table using insert command −

mysql> insert into paddingDemo values(78);
Query OK, 1 row affected (0.14 sec)

mysql> insert into paddingDemo values(560);
Query OK, 1 row affected (0.17 sec)

mysql> insert into paddingDemo values(888995);
Query OK, 1 row affected (0.13 sec)

mysql> insert into paddingDemo values(999994);
Query OK, 1 row affected (0.15 sec)

Following is the query to display all records from the table using select statement −

mysql> select * from paddingDemo;

This will produce the following output −

+--------+
| Number |
+--------+
| 78     |
| 560    |
| 888995 |
| 999994 |
+--------+
4 rows in set (0.00 sec)

Following is the query to pad the beginning of a MySQL INT field with zeroes −

mysql> select LPAD(CONVERT(Number,char(8)),10,'0') AS paddingWithZeros from
paddingDemo;

This will produce the following output −

+------------------+
| paddingWithZeros |
+------------------+
| 0000000078       |
| 0000000560       |
| 0000888995       |
| 0000999994       |
+------------------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements