Evan4623
Evan4623

Reputation: 259

leading zeros in mysql zerofill int field not showing when queried

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.

Upvotes: 3

Views: 9897

Answers (3)

Douglas Mott
Douglas Mott

Reputation: 1

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.

Upvotes: 0

anoldermark
anoldermark

Reputation: 381

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.

Upvotes: 0

Rajib Sarker
Rajib Sarker

Reputation: 91

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

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

Upvotes: 8

Related Questions