Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow number to start with ZERO when stored in mysql integer field

I need to store phone numbers starting with 0 but whenever i try to store this in MySql table the starting ZERO is removed because no number start with Zero actually.

How to solve this issue? Do I need to change the field type from Integer to another type?

like image 631
EzzDev Avatar asked May 07 '10 04:05

EzzDev


People also ask

Can integer start with 0 in SQL?

No, because leading zeros are meaningless on numeric data. If you have something that should have leading zeros, it should be stored as a string, because it's not numeric data. Or you should leave it as INT and format your output such that it displays in the format you require.

What is Zerofill in MySQL?

The ZEROFILL attribute is ignored for columns involved in expressions or UNION queries. If you store values larger than the display width in an integer column that has the ZEROFILL attribute, you may experience problems when MySQL generates temporary tables for some complicated joins.

What is integer in MySQL?

In MySQL, INTEGER (INT) is a numeric value without a decimal. It defines whole numbers that can be stored in a field or column. In addition, MySQL supports the display_width attribute (for example, INT(1)) and the ZEROFILL attribute, which automatically adds zeros to the value depending on the display width.


2 Answers

change data type to unsigned-zerofill whatever you are using, float, int, decimal(6,2)... only edit the field to unsigned-zerofill

like image 146
devasia2112 Avatar answered Sep 17 '22 14:09

devasia2112


Phone numbers are not really numbers in the sense that they aren't ordinal. They're just characters - they fact that they are numbers is incidental.

Store them in a varchar and move on :D

like image 24
Peter Bailey Avatar answered Sep 18 '22 14:09

Peter Bailey