Sometimes it is desperately required to format the phone number before displaying them in your website. In this ms sql server tutorial we will learn how to format the phone number in mssql server query.
Actually we want to convert telephone number like this 6092758415 into (609) 275-8415. Let’s have a look over how to do this, for example we have a column user_phone in our database table users, let’s look how to format the telephone number in mssql server query. It's easy and better to perform formatting of phone numbers in sql.
select case when len(ltrim(rtrim(User_Phone)))='10' then '('+SUBSTRING(User_Phone,1,3)+')'+' '+SUBSTRING(User_Phone,4,3)+'-'+SUBSTRING(User_Phone,7,4) when len(ltrim(rtrim(User_Phone)))='' then ' ' end AS User_Phone from users
Now phone number field will show data in this format (###) ###-####
I hope you will like this ms sql server tutorial to format the phone number in this format (###) ###-####
Related Posts
2 comments:
thanks
-- don't fubar the existing properly formated phone number
select case when len(ltrim(rtrim(User_Phone)))='10' then '('+SUBSTRING(User_Phone,1,3)+')'+' '+SUBSTRING(User_Phone,4,3)+'-'+SUBSTRING(User_Phone,7,4) else User_Phone end AS User_Phone from users
Post a Comment