0

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

2 Answers 2

1
concat(
    substring(account_id,1,3),
    '-',
    substring(account_id,4,3),
    '-',
    substring(account_id,7,4)
)
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried with 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?
I doubt there's a noticable difference. That will definitely be different if the value is longer or shorter than 10.
1

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
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.