Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

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


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 7:14 PM
Points: 8,200, Visits: 17,740
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
Post #1351728
Posted Wednesday, August 29, 2012 9:44 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, December 1, 2016 10:11 AM
Points: 8,587, Visits: 18,753
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 @ 2:49 PM
Points: 7,948, Visits: 19,043
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.

Just when you think your code is idiot-proof, the world invents a better idiot.
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: Friday, November 25, 2016 10:59 AM
Points: 366, Visits: 997
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


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 7:14 PM
Points: 8,200, Visits: 17,740
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
Post #1351817
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: Tuesday, November 22, 2016 1:08 AM
Points: 887, Visits: 1,787
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
Post #1351970
Posted Wednesday, August 29, 2012 11:16 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 9:09 PM
Points: 42,046, Visits: 39,429
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."

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: Tuesday, October 11, 2016 10:43 AM
Points: 1,145, Visits: 1,191
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
Posted Thursday, August 30, 2012 7:19 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 31, 2013 3:44 AM
Points: 314, Visits: 4,128
I guess if you absolutely must store a formatted phone number you could use a computed column.
Post #1352199
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse