常⽤函数:字符串函数
字符串函数
⼀、MySQL字符串函数
序号函数功能
1CONCAT(s1,)连接s1、s2...sn为⼀个字符串
2INSERT(str,x,y,instr)将字符串Str从第x位置开始,y个字符串长的⼦串替换为字符
串instr
3LOWER(str)将str字符串变为⼩写
4UPPER(str)将str字符串变为⼤写
5LEFT(str,x)返回字符串str最左边的x个字符
6RIGHT(str,x)返回字符串str最右边的x个字符
7LPAD(str,n,pad)⽤字符串pad对str最左边进⾏填充,直到长度为n个字符长度8RPAD(str,n,pad)⽤字符串pad对str最右边进⾏填充,直到长度为n个字符长度9LTRIM(str)去掉字符串str左侧的空格
10RTRIM(str)去掉字符串str右侧的空格
11TRIM(str)去掉字符串⾸尾的空格
12REPEAAT(str,x)返回str重复x次的结果
13REPLACE(str,a,b)⽤b字符串替换字符串str中所有出现的a
14STRCMP(s1,s2)⽐较字符串s1和s2
15SUBSTRING(str,x,y)返回从字符串x位置起y个字符串长度
1.1 CONCAT(s1,)
字符串拼接
注意:任何字符串与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>
转载需注明出处

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。