-
计算字符串长度
mysql> select length("你好");
+----------------+
| length("你好") |
+----------------+
| 4 |
+----------------+
2. 字符串的截取
mysql> select substring("hello",1,2);
+------------------------+
| substring("hello",1,2) |
+------------------------+
| he |
+------------------------+
3. 返回字符串出现的位置
mysql> select instr("name",'a');
+-------------------+
| instr("name",'a') |
+-------------------+
| 2 |
+-------------------+
综合应用
mysql> select substring("A001-14",1,instr("A001-14","-")-1);
+-----------------------------------------------+
| substring("A001-14",1,instr("A001-14","-")-1) |
+-----------------------------------------------+
| A001 |
+-----------------------------------------------+
4. 从左侧开始取子字符串
mysql> select left("A001-15",4);
+-------------------+
| left("A001-15",4) |
+-------------------+
| A001 |
+-------------------+
5.从右侧开始取子字符串
mysql> select right("A001-15",3);
+--------------------+
| right("A001-15",3) |
+--------------------+
| -15 |
+--------------------+
6. 字符串的替换
mysql> select replace("A001-15","A","B");
+----------------------------+
| replace("A001-15","A","B") |
+----------------------------+
| B001-15 |
+----------------------------+
删除字符串中间的空格
SELECT REPLACE(' abc 123 wpf',' ','') , REPLACE(' ccw enet wcf f',' ','');
转载请注明:谷谷点程序 » MYSQL 中字符串函数 归纳总结