Page 1 of 1

Preserving leading zeros

Posted: Tue Jul 28, 2009 12:23 pm
by steve9876
I've just created my first mysql database, and find that in a CHAR field that will be all digits, leading zeros are truncated.

I want to keep leading zeros. Is there another datatype or option that will do that?

Re: Preserving leading zeros

Posted: Tue Jul 28, 2009 3:08 pm
by tr0gd0rr
It looks like you want an INT field with ZEROFILL : "For example, for a column declared as INT(5) ZEROFILL, a value of 4 is retrieved as 00004."

Re: Preserving leading zeros

Posted: Tue Jul 28, 2009 4:20 pm
by steve9876
Thanks, but I came up with another solution: instead of generating a random number between 1000001 and 1999999 (for 6 digits), I generate one between 1100001 and 1999999. (Or maybe I could have just used 100001 - 999999.)

Re: Preserving leading zeros

Posted: Tue Jul 28, 2009 5:42 pm
by jayshields
Correct me if I'm wrong, but CHAR fields shouldn't strip leading zeros, but anyway, if you want to use any field which strips leading zeros, and you know how many digits the integer should be, you can just pad it out on/after retrieval.