mysqlstring-formattingstring-function

How to format a string value in MySQL?


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


Solution

  • concat(
        substring(account_id,1,3),
        '-',
        substring(account_id,4,3),
        '-',
        substring(account_id,7,4)
    )