NYCPHP Meetup

NYPHP.org

[nycphp-talk] TRUNCATING MySQL FIELDS

Michael Sims jellicle at gmail.com
Sat Jul 28 14:36:07 EDT 2007


On July 28, 2007, PaulCheung wrote:

> When I set the table up I set the LICENCE to INT(8). When populating this
> field I was expecting any number larger than 8 digits to be truncated and
> any number less than 8 digits to be padded out with zeros, in both cases
> this is not happening.

Nope.  An Int is an Int is an Int.  The number in parentheses specifies how 
large the zerofill will be, but, oddly, you have to specify the zerofill 
separately.

So if you declare a column as "int(5) zerofill", it means that it's an int 
(four bytes of storage, range 2^31 to -2^31), and if you output it and it's 
less than 5 decimal digits, it will be padded with zeroes up to five 
characters.

It's bad practice to intentionally try to truncate data when inserting it 
into a table.  I might come along later and fix your "bug" without 
understanding what you were trying to do.  Probably you should tell your 
random number generator to give you a number of exactly eight digits, if 
that's what you want.

Michael Sims




More information about the talk mailing list