What datatype should be used for storing phone numbers in SQL Server 2005?

前端 未结 16 1959
情话喂你
情话喂你 2020-11-28 05:29

I need to store phone numbers in a table. Please suggest which datatype should I use? Wait. Please read on before you hit reply..

This field needs

相关标签:
16条回答
  • 2020-11-28 05:45

    Use SSIS to extract and process the information. That way you will have the processing of the XML files separated from SQL Server. You can also do the SSIS transformations on a separate server if needed. Store the phone numbers in a standard format using VARCHAR. NVARCHAR would be unnecessary since we are talking about numbers and maybe a couple of other chars, like '+', ' ', '(', ')' and '-'.

    0 讨论(0)
  • 2020-11-28 05:46

    It is fairly common to use an "x" or "ext" to indicate extensions, so allow 15 characters (for full international support) plus 3 (for "ext") plus 4 (for the extension itself) giving a total of 22 characters. That should keep you safe.

    Alternatively, normalise on input so any "ext" gets translated to "x", giving a maximum of 20.

    0 讨论(0)
  • 2020-11-28 05:47

    Use CHAR(10) if you are storing US Phone numbers only. Remove everything but the digits.

    0 讨论(0)
  • 2020-11-28 05:47

    using varchar is pretty inefficient. use the money type and create a user declared type "phonenumber" out of it, and create a rule to only allow positive numbers.

    if you declare it as (19,4) you can even store a 4 digit extension and be big enough for international numbers, and only takes 9 bytes of storage. Also, indexes are speedy.

    0 讨论(0)
  • 2020-11-28 05:47

    Since you need to accommodate many different phone number formats (and probably include things like extensions etc.) it may make the most sense to just treat it as you would any other varchar. If you could control the input, you could take a number of approaches to make the data more useful, but it doesn't sound that way.

    Once you decide to simply treat it as any other string, you can focus on overcoming the inevitable issues regarding bad data, mysterious phone number formating and whatever else will pop up. The challenge will be in building a good search strategy for the data and not how you store it in my opinion. It's always a difficult task having to deal with a large pile of data which you had no control over collecting.

    0 讨论(0)
  • 2020-11-28 05:49

    Does this include:

    • International numbers?
    • Extensions?
    • Other information besides the actual number (like "ask for bobby")?

    If all of these are no, I would use a 10 char field and strip out all non-numeric data. If the first is a yes and the other two are no, I'd use two varchar(50) fields, one for the original input and one with all non-numeric data striped and used for indexing. If 2 or 3 are yes, I think I'd do two fields and some kind of crazy parser to determine what is extension or other data and deal with it appropriately. Of course you could avoid the 2nd column by doing something with the index where it strips out the extra characters when creating the index, but I'd just make a second column and probably do the stripping of characters with a trigger.

    Update: to address the AJAX issue, it may not be as bad as you think. If this is realistically the main way anything is done to the table, store only the digits in a secondary column as I said, and then make the index for that column the clustered one.

    0 讨论(0)
提交回复
热议问题