日期:2014-05-16  浏览次数:20992 次

mysql的字符串操作
有个字符串'34173cb38f07f89d',想把它每4位用空格隔开,请问用哪个函数?
想要的效果为:3417 3cb3 8f07 f89d

------解决方案--------------------
期待高人给出好办法。
下面用了三个函数,你也可以只用 substring 函数来做。

SQL code
mysql> select @str := "34173cb38f07f89d";
+----------------------------+
| @str := "34173cb38f07f89d" |
+----------------------------+
| 34173cb38f07f89d            |
+----------------------------+
1 row in set

mysql> select @str;
+------------------+
| @str             |
+------------------+
| 34173cb38f07f89d |
+------------------+
1 row in set
mysql> select @str, concat(left(@str, 4), " ", substring(@str, 4, 4), " ", substring(@str, 8, 4), " ", right(@str, 4)) as "new_str";
+------------------+---------------------+
| @str             | new_str             |
+------------------+---------------------+
| 34173cb38f07f89d | 3417 73cb 38f0 f89d |
+--+--+
1 row in set