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


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

9 comments:

Anonymous said...

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

Madhivanan said...

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

Anonymous said...

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

Rafael

Anonymous said...

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

Madhivanan said...

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

Anonymous said...

the stuff function is create thanks for the help

Anonymous said...

The STUFF function example is great thanks for the help

Unknown said...

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

Unknown said...

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...