leading zeros in mysql zerofill int field not showing when queried

Evan4623 picture Evan4623 · Sep 14, 2010 · Viewed 8.5k times · Source

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.

Answer

Rajib Sarker picture Rajib Sarker · Dec 22, 2012

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

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