Say there is a varchar column account_id all of them are 10 integer like 1234567890.
How can I format a value like 1234567890 to 123-456-7890 in mysql?
1234567890 => 123-456-7890
concat(
substring(account_id,1,3),
'-',
substring(account_id,4,3),
'-',
substring(account_id,7,4)
)
CONCAT(LEFT(account_id, 3),'-',LEFT(RIGHT(account_id, 7),3),'-', RIGHT(account_id, 4)). Could you please give me some advice about which one is better on execution considering?You also can use CONCAT_WS function:
SELECT CONCAT_WS('-',
LEFT(account_id, 3), -- first 3 symbols
MID(account_id, 4, LENGTH(account_id)-7), -- rest middle symbols
RIGHT(account_id, 4) -- last 4 symbols
);