0

假设有一个 varchar 列account_id,它们都是 10 个整数,例如1234567890. 如何在mysql中1234567890格式化一个值?123-456-7890


1234567890  => 123-456-7890

4

2 回答 2

1
concat(
    substring(account_id,1,3),
    '-',
    substring(account_id,4,3),
    '-',
    substring(account_id,7,4)
)
于 2020-06-21T06:55:38.400 回答
1

您还可以使用CONCAT_WS函数:

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
);
于 2020-06-21T09:36:53.387 回答