leading zeros in mysql zerofill int field not showing when queried

9.6k views Asked by At

I have a table with auto increment zerofill ID numbers. When I query the data the IDs lose their leading zeros (i.e. "000529" returns as "529"). Is there a way to preserve the leading zeros, or even generate them back in the query statement? I know I can generate them back in PHP using STRPAD, but for the specific project I am on I would like to retrieve the data as it is in the DB.

3

There are 3 answers

0
Douglas Mott On

When you select the field with leading zeros and use a UNION it will remove the leading zeros.
I found a workaround by adding:

CONVERT(id_number,char) as id_number

Obviously, id_number represents whatever zero filled field you are trying to SELECT.

1
Rajib Sarker On

Use function LPAD() to show the numbers (left) padded with zeros:

SELECT LPAD( 529, 6, '0') AS padded;
0
anoldermark On

It's the UNION ALL part that is converting them to INT - I've yet to find a simple solution. To my mind it's a mySql bug.