format for phone number

  • hi

    i need to craete table which has phone number field,

    format (345) 234 - 2345

    what is the best data type i can use,int,varchar.

    plz suggest me

  • It should be char or varchar.

    Ideally you should only store the digits and format the phone in the front end. However, you'll need an extra field for extension if needed.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Numeric datatypes are inappropriate because telephone numbers may have embedded spaces, non-numeric characters and leading zero's. Length isn't constant either, so most folks use VARCHAR.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Luis Cazares (8/29/2012)


    It should be char or varchar.

    Ideally you should only store the digits and format the phone in the front end. However, you'll need an extra field for extension if needed.

    I can imagine international numbers introducing difficulties to this solution (different display formats, therefore need to parse the country code first in a separate column etc etc).

    I'd stick with varchar & use suitable constraints and front-end validation to stop rubbish getting in there.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Luis Cazares (8/29/2012)


    It should be char or varchar.

    Ideally you should only store the digits and format the phone in the front end. However, you'll need an extra field for extension if needed.

    This. Believe me, you will be much happier writing code going forward when all you have to deal with is xxxxxxxxxx.

  • Phil Parkin (8/29/2012)


    Luis Cazares (8/29/2012)


    It should be char or varchar.

    Ideally you should only store the digits and format the phone in the front end. However, you'll need an extra field for extension if needed.

    I can imagine international numbers introducing difficulties to this solution (different display formats, therefore need to parse the country code first in a separate column etc etc).

    I'd stick with varchar & use suitable constraints and front-end validation to stop rubbish getting in there.

    Display format should be defined for each user, not each phone. If managing international numbers, there should be a column designed for it. I'll prefer to store just the digits and probably in separate fields (according to the different attributes) to allow easy formatting. For me a phone number could be a composite attribute or even an entity, however it can be handled as a simple attribute.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • In my shop we store the 10 digit us number as a VARCHAR(16) (to allow for international numbers) but do not store any country code information or formating with the phone number (xxxxxxxxxx). we look up the country code based on other information in the database.


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • harri.reddy (8/29/2012)


    hi

    i need to craete table which has phone number field,

    format (345) 234 - 2345

    what is the best data type i can use,int,varchar.

    plz suggest me

    I have to chime in with several of the others. Don't store formatted telephone numbers in the database. Do the formatting in the front end if you have one.

    A better thing to do would be to break the phone number up into pieces for Area Code, Exchange, and Line Number. Of course, international numbers will require country code (1-3 digits), city code (0 to 4 digits), and line number (1 to 6 digits, IIRC). Well, except for Mexico which uses country code, band, and some other stuff.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Use varchar . and don't save the formatted data in the database.

    I had faced this issue , and it was a hell of a mess when a new company was acquired and we started using the data of their end. Format of phone number may differ based on the area or region or company, and you might loose flexibility.

    why varchar ? I think it's better to do string operations on varchar in sql.

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • I guess if you absolutely must store a formatted phone number you could use a computed column.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply