How to keep leading Zero when inserting Int into field

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

How to keep leading Zero when inserting Int into field

Post by JayBird »

I have a field in a MySQL table that i have sent to Int(4)

The numbers are going to be starting at 0001 and going upwards.

But when i insert into the field, the leading Zero's get chopped off. How can i keep them?

Do i need to change the filed type?

Thanks

Mark
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

You could always add them back, no?

Code: Select all

$var = sprintf("%04s", $var);
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Post by maniac9 »

In the database, specify the int column as ZEROFILL - ie.

columnName INTEGER(4) ZEROFILL

and it will keep the zeros for you
Post Reply