cupojava1
cupojava1

Reputation: 41

Formating a phone number string in MySQL

I was wondering if there is a simpler way in MySQL to take a 10 digit phone number 9876543210 and display it in the format (987) 654-3210. I have code that works fine but I was wondering if there was another way perhaps similar to DATE_FORMAT().

The code I'm using:

IF(phone_cell != '',
    CONCAT('(',
            SUBSTRING(phone_cell, 1, 3),
            ') ',
            SUBSTRING(phone_cell, 4, 3),
            '-',
            SUBSTRING(phone_cell, 7, 4)),
    '') AS Mobile

The part I'm seeking is to replace the CONCAT statement.

Upvotes: 4

Views: 1537

Answers (2)

Lukasz Szozda
Lukasz Szozda

Reputation: 175716

You could use INSERT:

SELECT INSERT(INSERT(INSERT(s.num,1,0, '('), 5,0,') '),10,0,'-')
FROM (SELECT '9876543210' AS num) s;

DBFiddle Demo

Upvotes: 0

stackFan
stackFan

Reputation: 1608

No. Unfortunately, there is no such method as to format the phone number in MySQL. Your concat method is working fine.

Upvotes: 3

Related Questions