storing 0001 value in mysql but store as 1 why?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imrantareen
Forum Newbie
Posts: 1
Joined: Wed Apr 20, 2011 2:09 am

storing 0001 value in mysql but store as 1 why?

Post by imrantareen »

storing 0001 value in mysql but store as 1 why?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: storing 0001 value in mysql but store as 1 why?

Post by McInfo »

To store leading zeros, the field must have a string data type. If the field has a numeric data type, the leading zeros will be stripped off. This happens because leading zeros are insignificant to numbers. Their existence does not change the meaning of the number. However, leading zeros are significant to strings because removing a zero would change the meaning of the string.

In general, you should store the value as a number and add the leading zeros when you retrieve it. For example,

Code: Select all

SELECT LPAD(1, 4, '0') # Returns "0001"
Post Reply