假设有一个 varchar 列account_id
,它们都是 10 个整数,例如1234567890
. 如何在mysql中1234567890
格式化一个值?123-456-7890
1234567890 => 123-456-7890
假设有一个 varchar 列account_id
,它们都是 10 个整数,例如1234567890
. 如何在mysql中1234567890
格式化一个值?123-456-7890
1234567890 => 123-456-7890
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
);