Search code examples
sqlpostgresql

formating a phone number in the postresql query


Can you format a phone number in an postgreSQL query? I have a phone number column. The phone numbers are held as such: 1234567890. I am wondering if postgres will format to (123) 456-7890. I can do this outside the query, I am using php, but it would be nice if I was able to have the output of the query like (123) 456-7890


Solution

  • Use SUBSTRING function

    something like:

      SELECT 
     '(' || SUBSTRING((PhoneNumber, 1, 3) + ') ' || SUBSTRING(PhoneNumber, 4,3) || '-' || SUBSTRING((PhoneNumber,7,4)