November 02, 2010

Format Phone Numbers in SQL Server




Formatting should be done using your front end application. However if that option does not exist due to some reason, use the following T-SQL code to format Phone Numbers in SQL Server.

Note: The code shown below formats US Phone Numbers. I have covered three scenarios, however feel free to modify the code to add additional scenarios.

The 3 possible scenarios of a US Phone number input could be:

18052224353 will be returned as 1 (805) 222-4353
8052224353 will be returned as (805) 222-4353
2224353 will be returned as 222-4353

SELECT
phno,
CASE LEN(phno)
WHEN 11 THEN LEFT(phno,1)+
STUFF(STUFF(STUFF(phno,1,1,' ('),6,0,') '),11,0,'-')
WHEN 10 THEN
STUFF(STUFF(STUFF(phno,1,0,' ('),6,0,') '),11,0,'-')
WHEN 7 THEN
STUFF(phno,4,0,'-')
ELSE 'Invalid phno'
END as formatted_phno
FROM
(
SELECT
'18052224353' as phno union all
SELECT
'8052224353' as phno union all
SELECT
'888052224353' as phno union all
SELECT
'2224353' as phno
)
as t

The SQL Server STUFF function is used to replace certain characters with other characters, based on the position. For example STUFF(phno,1,1,'(') replaces the first character with '('. Similarly STUFF(phno,6,1,') ') inserts ') ' after position 6.

OUTPUT

image


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at www.sqlteam.com and a moderator at www.sql-server-performance.com. His T-sql blog is at http://beyondrelational.com/blogs/madhivanan

 
  Feedback:

comments

9 Responses to "Format Phone Numbers in SQL Server"
  1. Anonymous said...
    November 2, 2010 at 9:32 PM

    i was searching for same solution. Can you show to write update query for this. The number in column PhoneNos nvarchar is like 5551212 and I want to format this column with your algorithm. plz help

    Rafael

  2. Madhivanan said...
    November 2, 2010 at 11:53 PM

    For updating a table, the code should be

    update table
    set phno=
    CASE LEN(phno)WHEN 11 THEN LEFT(phno,1)+ STUFF(STUFF(STUFF(phno,1,1,' ('),6,0,') '),11,0,'-')WHEN 10 THEN STUFF(STUFF(STUFF(phno,1,0,' ('),6,0,') '),11,0,'-')WHEN 7 THEN STUFF(phno,4,0,'-')ELSE phno END


    I have given phno instead of 'Invalid format' so that your original data will be there

  3. Anonymous said...
    November 8, 2010 at 12:37 AM

    many many thank you for giving update table code. it worked perfectly

    Rafael

  4. Anonymous said...
    November 26, 2010 at 1:47 AM

    brilliant article on Phone number formatting. Can you also show me how to do it for India phone number

    I have this 0091XXXXXXXXXX and I want to convert it to +91-XXXXXXXXXX

  5. Madhivanan said...
    November 26, 2010 at 3:28 AM

    Here is the one that works for Indian format


    select
    phno,
    case LEN(phno)
    when 14 then '+'+STUFF(STUFF(phno,1,2,''),3,0,'-')
    when 10 then phno
    else 'Invalid phno'
    end as formatted_phno
    from
    (
    select
    '00919443857375' as phno union all
    select
    '9447532568' as phno union all
    select
    '00919443858712' as phno union all
    select
    '2224353' as phno )
    as t

  6. Anonymous said...
    August 9, 2012 at 10:59 AM

    the stuff function is create thanks for the help

  7. Anonymous said...
    August 9, 2012 at 11:00 AM

    The STUFF function example is great thanks for the help

  8. Allavali Pinjari said...
    January 26, 2014 at 11:56 PM

    I need the below format for displaying below:
    if the Length of the Phone no.is 10 then (123) 234-1234
    If the len(Phno) >10 then
    (123) 123-234 x1234455
    Can you please help me on this.

    Thanks,
    Vali

  9. Stephen Chilcoat said...
    January 29, 2014 at 9:43 AM

    It seems this query is only selecting three specific phone numbers and updating them. How do I select 15,000 phone numbers that are in the wrong format and correct the format on all of them? I can't put each one in my SELECT criteria...

 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions