在mysql select语句中,每隔2个字符在字符串中添加连字符
我有一个mysql表和一些数据。因为我有一个列(mobile_number),该列的值类似于mobile number(例如:1234587920) 我的预期产量是:12-34-58-79-20 我想在每两个数字后面加上连字符 你可以试试这个在mysql select语句中,每隔2个字符在字符串中添加连字符,mysql,sql,Mysql,Sql,我有一个mysql表和一些数据。因为我有一个列(mobile_number),该列的值类似于mobile number(例如:1234587920) 我的预期产量是:12-34-58-79-20 我想在每两个数字后面加上连字符 你可以试试这个 select concat( right(phone_number,3) , "-" , mid(phone_number,4,3) , "-", right(phone_number,4)) from table 这将有助于: SELECT conca
select concat( right(phone_number,3) , "-" , mid(phone_number,4,3) , "-", right(phone_number,4)) from table
这将有助于:
SELECT concat(SUBSTRING("1234587920", 1, 2),"-",SUBSTRING("1234587920",3,2),"-
",SUBSTRING("1234587920", 5, 2),"-",
SUBSTRING("1234587920", 7, 2),"-",
SUBSTRING("1234587920", 9, 2)) AS ExtractString;
对于您的表,查询如下所示:
SELECT concat(SUBSTRING(mobile_number, 1, 2),"-
",SUBSTRING(mobile_number,3,2),"-",SUBSTRING(mobile_number, 5, 2),"-",
SUBSTRING(mobile_number, 7, 2),"-",
SUBSTRING(mobile_number, 9, 2)) ExtractString from tablename;
在mysql中,使用concat()连接字符串,使用substr()拆分字符串:
拆分(列、开始、计数)从开始位置为计数字符选择列
concat(第1列、“-”、第2列)将是第1-2列
SELECT CONCAT(SUBSTR(phone,1,2),'-',SUBSTR(phone,3,2),'-',SUBSTR(phone,5,2),'-
',SUBSTR(phone,7,2),'-',SUBSTR(phone,9,2)) FROM `table`
我肯定会为此使用
concat\u ws()
:
select concat_ws('-',
substr(mobile_number, 1, 2),
substr(mobile_number, 3, 2),
substr(mobile_number, 5, 2),
substr(mobile_number, 7, 2)
)
这不是一个命令。在演示层做这样的事情似乎要聪明得多,不要做评判。一点也不谢谢esnkrimi…我想再问一个问题…如果我不知道我现在有多少个数字(即字符串长度),我有10个数字,所以我把它作为静态的,但如果它是动态的,解决方案是什么?