63

As the question states, why is it considered best practice to store telephone numbers as strings rather than integers in the telephone_number column?

Not sure I understand the rationale for this. Please help clear this up!

Thanks!

1
  • This was a fun Q/A! I love the confusion caused by not specifying that "phone numbers are strings of digit characters we represent with integers". I'm guessing the characters used "under the covers" are represented by integers because they map to integers (bijection). I'm seeing similar confusion popping up in a lot of things due to imprecise natural language. So phone numbers are integers and they're not, depending on the context.
    – DukeZhou
    May 3, 2018 at 19:20

5 Answers 5

116

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

2
  • 1
    For user's phone numbers, would you always have a single field? if so, would that also be the case for international numbers?
    – stevec
    Sep 24, 2020 at 4:26
  • 2
    @stevec: That's partly up to you and a different question I think. For most use cases, I would personally use a single field and a library that could parse the numbers to separate out dial codes etc if it was important, because I would not want to put that requirement on users of a system to put the correct parts into the right boxes in a multi-field version. However, in some use cases it might be convenient to separate into logical components on input or when storing the number. Sep 24, 2020 at 6:55
10

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.

9

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.

2
  • Upvoted because I have written one and working with phone numbers as integers in the database is far faster. The downside of course is weird looking arithmetic when you need the NXX or 1000 block ID.
    – Josh
    May 26, 2016 at 9:35
  • Upvoted since I've implemented multiple projects at large Telcos and fully agree with your experience. Sep 26, 2016 at 14:58
7

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

4

There is several reasons for this :

  • Phone numbers often start with a "0" : an integer will remove all leading "0"s
  • Phone number can have special char : +, (, -, etc. (for exemple : +33 (0)6 12 23 34)
  • You cannot perform operations on phones : adding phones, for instance, would be meaningless
  • Phone number may be internationalised, i.e. different format for different people, thus not possible with integers

There might be other reasons, but I guess that's already a fair amount of those :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.