【MySQL】内置函数——字符串函数

函数名称 说明
charset(str) 返回字符串的字符集
concat(string1,…) 拼接字符串
instr(string,substring) 返回substring在string中出现的位置
ucase(string) 转换成大写
lcase(string) 转换成小写
left(string,length) 从左往右截取string的length个字符
right(string,length) 从右往左截取string的length个字符
length(string) string的长度
replace(str,search_str,replace_str) 在str中用replace_str替换search_str
substring(string,position,length) 从str的position开始,取length个字符
strcmp(string1,string2) 逐字符比较两字符串大小
ltrim(string)/rtrim(string)/trim(string) 去除左空格/去除右空格/去除左右空格

  • 查看字符串的字符集——charset()
mysql> select ename from emp;
+--------+
| ename  |
+--------+
| SMITH  |
| ALLEN  |
| WARD   |
| JONES  |
| MARTIN |
| BLAKE  |
| CLARK  |
| SCOTT  |
| KING   |
| TURNER |
| ADAMS  |
| JAMES  |
| FORD   |
| MILLER |
+--------+

mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
+----------------+

  • 拼接字符串——concat()
mysql> select concat(deptno,'号部门的',ename,'的薪资是',sal) as 薪资 from emp limit 2;
+----------------------------------------+
| 薪资                                   |
+----------------------------------------+
| 20号部门的SMITH的薪资是800.00          |
| 30号部门的ALLEN的薪资是1600.00         |
+----------------------------------------+

  • string的长度——length()
mysql> select ename,length(ename) from emp limit 3;
+-------+---------------+
| ename | length(ename) |
+-------+---------------+
| SMITH |             5 |
| ALLEN |             5 |
| WARD  |             4 |
+-------+---------------+

注意:length函数返回字符串长度,以字节为单位,如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。比如:字母,数字算作一个字节,中文算作多个字节(gbk中2字节,utf8中3字节)


  • 字符串指定替换——replace()
mysql> select ename from emp;
+--------+
| ename  |
+--------+
| SMITH  |
| ALLEN  |
| WARD   |
| JONES  |
| MARTIN |
| BLAKE  |
| CLARK  |
| SCOTT  |
| KING   |
| TURNER |
| ADAMS  |
| JAMES  |
| FORD   |
| MILLER |
+--------+
14 rows in set (0.00 sec)

mysql> select replace(ename,'S','上海') from emp;
+-----------------------------+
| replace(ename,'S','上海')   |
+-----------------------------+
| 上海MITH                    |
| ALLEN                       |
| WARD                        |
| JONE上海                    |
| MARTIN                      |
| BLAKE                       |
| CLARK                       |
| 上海COTT                    |
| KING                        |
| TURNER                      |
| ADAM上海                    |
| JAME上海                    |
| FORD                        |
| MILLER                      |
+-----------------------------+

replace的替换会形成一张新表,原表数据并不会改变


  • 截取子字符串——substring()
  • 从左往右截取——left()
  • 从右往左截取——right()
//从ename属性数据的第二个字符开始,截取两个字符
mysql> select substring(ename,2,2) from emp limit 4;
+----------------------+
| substring(ename,2,2) |
+----------------------+
| MI                   |
| LL                   |
| AR                   |
| ON                   |
+----------------------+

mysql> select left(ename,2) from emp limit 4;
+---------------+
| left(ename,2) |
+---------------+
| SM            |
| AL            |
| WA            |
| JO            |
+---------------+

mysql> select right(ename,2) from emp limit 4;
+----------------+
| right(ename,2) |
+----------------+
| TH             |
| EN             |
| RD             |
| ES             |
+----------------+

转换成大写——ucase()
转换成小写——lcase()

mysql> select ucase('hello');
+----------------+
| ucase('hello') |
+----------------+
| HELLO          |
+----------------+
1 row in set (0.00 sec)

mysql> select lcase(ename) from emp limit 4;
+--------------+
| lcase(ename) |
+--------------+
| smith        |
| allen        |
| ward         |
| jones        |
+--------------+

查询字符位置——instr()
注意:下标从1开始,没有则返回0

mysql> select instr('hello','ll');
+---------------------+
| instr('hello','ll') |
+---------------------+
|                   3 |
+---------------------+

mysql> select instr('hello','le');
+---------------------+
| instr('hello','le') |
+---------------------+
|                   0 |
+---------------------+

逐字节比较两字符串大小——strcmp()
大于返回1,小于返回-1

mysql> select strcmp('hello','hl');
+----------------------+
| strcmp('hello','hl') |
+----------------------+
|                   -1 |
+----------------------+

mysql> select strcmp('hello','ha');
+----------------------+
| strcmp('hello','ha') |
+----------------------+
|                    1 |
+----------------------+

去除左空格——ltrim()
去除右空格——rtrim()
去除左右空格——trim()

//去除左空格
mysql> select ('     你好   ') 空格 ;
+----------------+
| 空格           |
+----------------+
|      你好      |
+----------------+

mysql> select ltrim('     你好   ') 空格 ;
+-----------+
| 空格      |
+-----------+
| 你好      |
+-----------+

//去除右空格
mysql> select rtrim('     你好   ') 空格 ;
+-------------+
| 空格        |
+-------------+
|      你好   |
+-------------+

//去除左右空格
mysql> select trim('     你好   ') 空格 ;
+--------+
| 空格   |
+--------+
| 你好   |
+--------+

你可能感兴趣的:(MySQL,mysql,android,数据库)