提问者:小点点

如何在MySQL中格式化字段值?


假设有一个varchar列account_id,它们都是10个整数,类似于1234567890。 如何在MySQL中将1234567890这样的值格式化为123-456-7890


1234567890  => 123-456-7890


共2个答案

匿名用户

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

匿名用户

还可以使用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
);