假设有一个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
);