Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it best to store a telephone number as a string vs. integer?

People also ask

Should you store phone numbers int or string?

Mobile phone numbers are not stored as integers, as the integer data type holds values that have the potential to be used in calculations. There is no context for using a mobile phone number as part of a calculation, so it is stored as a STRING value.

Should you store phone number as string?

I recommend using a string since that gives you more flexibility when it comes to formatting and non numeric characters like extension etc. Show activity on this post. I would suggest using String - aside from anything else, otherwise you won't be able to store leading zeroes.

Why is a telephone number normally stored as a string data type?

Telephone numbers need to be stored as a text/string data type because they often begin with a 0 and if they were stored as an integer then the leading zero would be discounted.

Which data type should be used to store a telephone number?

I generally use VARCHARs to store telephone numbers. Storage is not so expensive these days that I benefit that much from the savings found by storing them as numeric values.


Telephone numbers are strings of digit characters, they are not integers.

Consider for example:

  • Expressing a telephone number in a different base would render it meaningless

  • Adding or multiplying two telephone numbers together, or any math operation on a phone number, is meaningless. The result is not another telephone number (except by conicidence)

  • Telephone numbers are intended to be entered "as-is" into a connected device.

  • Telephone numbers may have leading zeroes.

  • Manipulations of telephone numbers, such as adding an area code, are String operations.

Storing the string version of the telephone number makes this clear and unambiguous.


History: On old pulse-encoded dial systems, the code for each digit in a telephone number was sent as the same number of pulses as the digit (or 10 pulses for "0"). That may be why we still use digits to represent the parts of a phone number. See http://en.wikipedia.org/wiki/Pulse_dialing


What Neil Slater said is correct. I would add that there are lots of edge cases where you can't express a telephone number as a number value consistently.

For example, consider these numbers:

011-123-555-1212
+11-123-555-1212
+1 (112) 355-5121 x2

These are all potentially valid phone numbers, but they mean very different things. Yet, in integer form, they are all 111235551212.


Consider these phone numbers for example

099-1234-56789 or +91-8907-687665.

In this case,if the phone_number attribute is of type integer,then it can't accept these values.It should be a string to hold these type of values.So string is always preferred than integer


If you are going to store the number for display from input, then you must use a string.

However, while it is true that no mathematical operations can be performed on a number that have meaning. Using a number in hashsets and for indexing is quicker than using a string. So provided you can guarantee or homogenise your set of numbers, so they are all consistent, then you may see better performance operating on a number.

For example, in the Telco world, rating calls for a given customer includes a lot of searching on their CLI and in this situation it is faster and cheaper to search by integer. Generally though strings will be fine performance wise, it is only where performance matters and you have multiple searches to perform for a huge range of numbers - i.e. Rating 250 million calls across 2 million lines and 2000 tariffs. In memory rating also gets expensive, so being able to use a 64bit int or uint is cheaper when dealing with these volumes.