Pad the values of the column with zeros in MySQL


For this, use the concept of ZEROFILL. It pads the displayed value of the field with zeros up to the display width set in the column definition

Let us first create a table −

mysql> create table DemoTable626 (Value int(5) zerofill);
Query OK, 0 rows affected (0.71 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable626 values(9);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable626 values(12);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable626 values(567);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable626 values(3478);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable626;

This will produce the following output −

+-------+
| Value |
+-------+
| 00009 |
| 00012 |
| 00567 |
| 03478 |
+-------+
4 rows in set (0.00 sec)

Updated on: 23-Aug-2019

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements