常用函数:字符串函数
字符串函数
一、MySQL字符串函数
序号 |
函数 |
功能 |
1 |
CONCAT(s1,s2,s3...) |
连接s1、s2...sn为一个字符串 |
2 |
INSERT(str,x,y,instr) |
将字符串Str从第x位置开始,y个字符串长的子串替换为字符串instr |
3 |
LOWER(str) |
将str字符串变为小写 |
4 |
UPPER(str) |
将str字符串变为大写 |
5 |
LEFT(str,x) |
返回字符串str最左边的x个字符 |
6 |
RIGHT(str,x) |
返回字符串str最右边的x个字符 |
7 |
LPAD(str,n,pad) |
用字符串pad对str最左边进行填充,直到长度为n个字符长度 |
8 |
RPAD(str,n,pad) |
用字符串pad对str最右边进行填充,直到长度为n个字符长度 |
9 |
LTRIM(str) |
去掉字符串str左侧的空格 |
10 |
RTRIM(str) |
去掉字符串str右侧的空格 |
11 |
TRIM(str) |
去掉字符串首尾的空格 |
12 |
REPEAAT(str,x) |
返回str重复x次的结果 |
13 |
REPLACE(str,a,b) |
用b字符串替换字符串str中所有出现的a |
14 |
STRCMP(s1,s2) |
比较字符串s1和s2 |
15 |
SUBSTRING(str,x,y) |
返回从字符串x位置起y个字符串长度 |
1.1 CONCAT(s1,s2,s3...)
- 字符串拼接
注意:任何字符串与null拼接结果都为null
mysql> select concat('a','b','c'),concat('a',null,'c'); +---------------------+----------------------+ | concat('a','b','c') | concat('a',null,'c') | +---------------------+----------------------+ | abc | NULL | +---------------------+----------------------+ 1 row in set (0.00 sec) mysql>
1.2 INSERT(str,x,y,instr)
- 字符串指定替换,将字符串Str从第x位置开始,y个字符串长的子串替换为字符串instr
mysql> select insert('nanning2020',8,4,'南宁'); +------------------------------------+ | insert('nanning2020',8,4,'南宁') | +------------------------------------+ | nanning南宁 | +------------------------------------+ 1 row in set (0.00 sec) mysql>
1.3 LOWER(str)
- 将str字符串变为小写
mysql> select lower('ABCD'); +---------------+ | lower('ABCD') | +---------------+ | abcd | +---------------+ 1 row in set (0.00 sec) mysql>
1.4 UPPER(str)
- 将str字符串变为大写
mysql> select upper('abcd'); +---------------+ | upper('abcd') | +---------------+ | ABCD | +---------------+ 1 row in set (0.00 sec) mysql>
1.5 LEFT(str,x)
- 返回字符串str最左边的x个字符
mysql> select left('12345',3); +-----------------+ | left('12345',3) | +-----------------+ | 123 | +-----------------+ 1 row in set (0.00 sec) mysql>
1.6 RIGHT(str,x)
- 返回字符串str最右边的x个字符
mysql> select right('12345',3); +------------------+ | right('12345',3) | +------------------+ | 345 | +------------------+ 1 row in set (0.00 sec) mysql>
1.7 LPAD(str,n,pad)
- 用字符串pad对str最左边进行填充,直到总长度为n个字符长度
注意:n表示计算的长度,是填充后的字符串总长度,不是pad的长度
mysql> select lpad('1234',8,'Q'); +--------------------+ | lpad('1234',8,'Q') | +--------------------+ | QQQQ1234 | +--------------------+ 1 row in set (0.00 sec) mysql>
1.8 RPAD(str,n,pad)
- 用字符串pad对str最右边进行填充,直到总长度为n个字符长度
注意:n表示计算的长度,是填充后的字符串总长度,不是pad的长度
mysql> select rpad('12345',8,'Q'); +---------------------+ | rpad('12345',8,'Q') | +---------------------+ | 12345QQQ | +---------------------+ 1 row in set (0.00 sec) mysql> select length(rpad('12345',8,'Q')); +-----------------------------+ | length(rpad('12345',8,'Q')) | +-----------------------------+ | 8 | +-----------------------------+ 1 row in set (0.00 sec) mysql>
1.9 LTRIM(str)
- 去掉字符串str左侧的空格
mysql> select ltrim(' abc'); +-----------------+ | ltrim(' abc') | +-----------------+ | abc | +-----------------+ 1 row in set (0.00 sec) mysql>
1.10 RTRIM(str)
- 去掉字符串str右侧的空格
mysql> select Rtrim('abc '); +-----------------+ | Rtrim('abc ') | +-----------------+ | abc | +-----------------+ 1 row in set (0.00 sec) mysql>
1.11 TRIM(str)
- 去掉字符串str两边的空格
mysql> select trim(' abc '); +-------------------+ | trim(' abc ') | +-------------------+ | abc | +-------------------+ 1 row in set (0.00 sec) mysql>
1.12 REPEAAT(str,x)
- 返回str重复x次的结果
mysql> select repeat('a',5); +---------------+ | repeat('a',5) | +---------------+ | aaaaa | +---------------+ 1 row in set (0.00 sec) mysql>
1.13 REPLACE(str,a,b)
- 用b字符串替换字符串str中所有出现的a
mysql> select replace('abbc','b','QQQ'); +---------------------------+ | replace('abbc','b','QQQ') | +---------------------------+ | aQQQQQQc | +---------------------------+ 1 row in set (0.00 sec) mysql>
1.14 STRCMP(s1,s2) --string compare
- 比较字符串s1和s2
- 比较实质:比较s1和s2的ascII码值大小。s1>s2,返回1 ,s1=s2则返回0,s1<s2返回-1
mysql> select strcmp('a','b'),strcmp('a','b'),strcmp('b','a'); +-----------------+-----------------+-----------------+ | strcmp('a','b') | strcmp('a','b') | strcmp('b','a') | +-----------------+-----------------+-----------------+ | -1 | -1 | 1 | +-----------------+-----------------+-----------------+ 1 row in set (0.00 sec) mysql> mysql> select ascii('a'),ascii('b'); +------------+------------+ | ascii('a') | ascii('b') | +------------+------------+ | 97 | 98 | +------------+------------+ 1 row in set (0.00 sec) mysql>
1.15 SUNDTRING(str,x,y)
- 返回从字符串x位置起y个字符串长度
mysql> select substring('123456789',3,4); +----------------------------+ | substring('123456789',3,4) | +----------------------------+ | 3456 | +----------------------------+ 1 row in set (0.00 sec) mysql>