1

I am using SQL Server 2008 and I found this solution which is only for SQL Server 2012 - what is the equivelant to format a phone number in SQL Server 2008?

FORMAT([PhoneField],'###-###-####')
  • 2
    There isn't one (and FORMAT() isn't a bowl of ice cream either - lots of overhead). Why do you need to format phone numbers in T-SQL? This is a job better handled by the presentation tier. – Aaron Bertrand Jan 19 '16 at 22:41
3

there isn't a function that will do it for you. You can cast the phone number as a string then then parse out the parts and add hyphen

declare @phone int
set @phone = '1232223333'
select SUBSTRING(cast(@phone as varchar(10)),1,3)+'-'+SUBSTRING(cast(@phone as varchar(10)),4,3)+'-'+SUBSTRING(cast(@phone as varchar(10)),7,4)
  • I would assume that PhoneField is a varchar, not an int. – ypercubeᵀᴹ Jan 21 '16 at 10:55
6

Assuming that the [PhoneField] is 10 digits, such as '1234567890' then you could use STUFF to put the proper values in. E.g.

select STUFF(STUFF('1234567890',7,0,'-'),4,0,'-');

But please note that this only answers formatting a phone number for the 10-digit answers. If you are using internationally dialed numbers, you need to investigate the various forms of phone numbers you might need to handle.

See: https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.