What's the best way to store a phone n开发者_开发问答umber for an app that'll store phones from different countries.
I'm thinking about storing the country code as an int and storing the phone number as a string (and at the application level only allow for digits in that string).
I'm using sql server
Are the phone numbers going to be programmatically used or is this just meant for showing them on the screen or printing them? If it's the latter, I'd simply use an nvarchar. The reason for this is that People from different countries have different habits of printing phone numbers.
For example US numbers look something like this:
xxx-xxxx-xxxx
While people in Germany often write
(x)xxx/xxxxxxx
Phone numbers are more complex than you'd think. For example, Linode lists this:
Phone: (609) 593-7103
Fax: (615) 250-4945
SIP: 0@office.linode.com
ISN: 0*862 (FWD, dial **012 first)
So how you store them is going to depend on what they are for. I'd keep it as simple as possible: if you're just storing "contact information", I'd keep it a free-format nvarchar
, and let the users reading it decide how to interpret it. In 2020, people will have phone numbers like:
1234567890@pstngw.yourisp.net
bart@simpson.com
1@whitehouse.gov
On the other hand, if you have a specific goal for the phone number, ensure the data is consistent with that goal. Say you have to control a dialing system for automatic dial-out. Then you should consult the manual to see what it accepts as valid phone numbers.
In general, don't add complexity before you're sure some benefit will result from it.
I would use a
varchar
and select an appropriate length (eg: 32/64/etc..)
not a numeric type since you are not going to do any math with it.
Depending on how the app is set up, you may be able to have a numeric phone number field, and keep track of type of number based on the country - that would allow you to control the mask for display purposes.
If you do store it as a string, you'll find that everyone has a different way of doing phone numbers. Even in North America, you'll find (xxx) xxx-xxxx, xxx-xxx-xxxx, xxx.xxx.xxxx, etc.
精彩评论