How do you fill in or pad a column with zeros using a MySQL query?

MySQLMySQLi Database

You can use ZEROFILL for column to fill in or pad with zeros. Let us first create a table−

mysql> create table DemoTable
   (
   Number int
   );
Query OK, 0 rows affected (0.58 sec)

Following is the query to add zerofill attribute for Number column−

mysql> alter table DemoTable change Number Number int(10) zerofill not null;
Query OK, 0 rows affected (1.13 sec)
Records: 0 Duplicates: 0 Warnings: 0

Insert records in the table using insert command −

mysql> insert into DemoTable values(1);
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable values(12);
Query OK, 1 row affected (0.53 sec)
mysql> insert into DemoTable values(123);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(1234);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(12345);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output−

+------------+
| Number     |
+------------+
| 0000000001 |
| 0000000012 |
| 0000000123 |
| 0000001234 |
| 0000012345 |
+------------+
5 rows in set (0.00 sec)
raja
Published on 03-May-2019 11:23:21
Advertisements