- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements