SQL Server – Formatting Phone Numbers

sql serversql-server-2008t-sql

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],'###-###-####')

Best Answer

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)