Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

format for phone number Expand / Collapse
Author
Message
Posted Wednesday, August 29, 2012 9:32 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 21, 2013 12:01 PM
Points: 149, Visits: 346
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
Post #1351709
Posted Wednesday, August 29, 2012 9:43 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:42 PM
Points: 3,576, Visits: 8,026
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1351728
Posted Wednesday, August 29, 2012 9:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 6,721, Visits: 13,832
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1351729
Posted Wednesday, August 29, 2012 10:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:58 PM
Points: 5,078, Visits: 11,862
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.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1351761
Posted Wednesday, August 29, 2012 12:01 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Saturday, September 13, 2014 10:46 AM
Points: 356, Visits: 912
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.
Post #1351803
Posted Wednesday, August 29, 2012 12:14 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:42 PM
Points: 3,576, Visits: 8,026
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1351817
Posted Wednesday, August 29, 2012 12:34 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, September 19, 2014 5:15 PM
Points: 1,945, Visits: 3,008

some_phone_nbr CHAR(11) NOT NULL
CHECK (some_phone_nbr LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')

The standard for phone number formatting is set by the International Telecommunication Union in [E.123] and [E.164] (see the references below). The standards documents are available for a fee from the ITU [available at no charge since 2010 --Bob.] . A summary is available in the Google (UseNet) discussion group, titled Need ITU-T E.123 summary.

In short, a North American telephone number should look like:

+C-AAA-PPP-NNNN;ext=xxxx

“+” shows where the dialing prefix goes. This is one of either the International Direct Dialing (IDD) prefix (for Canada this is “011″ for overseas dialing) or the National Direct Dialing (NDD) prefix (“1″ for calls within North America, omitted for toll-free calls),
“C” is the Country Code (North America’s CC is “1″, and it is omitted for dialing within North America),
“AAA” is the area code (always required for dialing in Kitchener, Toronto, and other jurisdictions),
“PPP” is the Exchange (or Private Branch Exchange “PBX”; look in the phone book to see which exchanges are supported),
“NNNN” is the local portion of the number,

Note that the sequence “AAA-PPP-NNNN” is called a “local number” and “+C-AAA-PPP-NNNN” is called a “global number”. The “-” (hyphen) is a visual separator, as are “.” (period) , “(” (left bracket) and “)” (right bracket), which dialing applications should ignore.



Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1351832
Posted Wednesday, August 29, 2012 6:43 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, September 19, 2014 5:16 AM
Points: 887, Visits: 1,774
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 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

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

Jeremy Oursler
Post #1351970
Posted Wednesday, August 29, 2012 11:16 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:46 PM
Points: 35,218, Visits: 31,676
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1352006
Posted Wednesday, August 29, 2012 11:55 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 31, 2014 7:15 PM
Points: 1,129, Visits: 1,163
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
Post #1352015
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse