What is the usage of ZEROFILL for INT datatype?


On specifying ZEROFILL for a numeric column, MYSQL automatically pads the displayed value of the field with zeros up to the display width specified in the column definition.

For example, we create a table showzerofill and insert the values as follows −

mysql> Create Table showzerofill(Val1 INT(5) ZEROFILL, Val2 INT(5));
Query OK, 0 rows affected (0.09 sec)

mysql> Insert into showzerofill(Val1, Val2)
values(1,1>,<12,12>,<123,123>,<1234,1234>,<12345,12345>;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0

Now we can easily understand the effect of ZEROFILL on the values of column Val1.

ZEROFILL padded zeros in the number up to the display width specified in the column definition

mysql> Select * from showzerofill;
+-------+-------+
| Val1  | Val2  |
+-------+-------+
| 00001 |     1 |
| 00012 |    12 |
| 00123 |   123 |
| 01234 |  1234 |
| 12345 | 12345 |
+-------+-------+
5 rows in set (0.00 sec)

In this sense, we can say that if we want to display the value of a column with a particular width then we can define that column with ZEROFILL.

Updated on: 28-Jan-2020

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements