I have table in the database with a phone number column. The numbers look like this:
123456789
I want to format that to look like this:
123-456-789
I have table in the database with a phone number column. The numbers look like this:
123456789
I want to format that to look like this:
123-456-789
If you want to just format the output no need to create a new table or a function. In this scenario the area code was on a separate fields. I use
field1
,field2
just to illustrate you can select other fields in the same query:Select statement:
Sample OUTPUT:
Updated @sqiller's function for my purposes
You can also try this:
end
use on it select
Output will be
As Above users mentioned, those solutions are very basic and they won't work if database has different phone formats like: (123)123-4564 123-456-4564 1234567989 etc
Here is a more complex solution that will work with ANY input given:
Solutions that use
SUBSTRING
and concatenation+
are nearly independent of RDBMS. Here is a short solution that is specific to SQL Server:This should do it:
Incorporated Kane's suggestion, you can compute the phone number's formatting at runtime. One possible approach would be to use scalar functions for this purpose (works in SQL Server):