1.concat函数的使用
concat(str1,str2,...)
返回的结果是参数连接后产生的字符串,如果有任何一个参数为null,则返回结果为null。
mysql> select concat('My','s','ql'); +-----------------------+ | concat('My','s','ql') | +-----------------------+ | Mysql | +-----------------------+ 1 row in set (0.00 sec) mysql> select concat('My','s','ql',null); +----------------------------+ | concat('My','s','ql',null) | +----------------------------+ | NULL | +----------------------------+ 1 row in set (0.00 sec) 该函数的作用就是连接字符串
2.concat_ws() :concat with separator 是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。
mysql> select concat_ws('@','My','s','ql'); +------------------------------+ | concat_ws('@','My','s','ql') | +------------------------------+ | My@s@ql | +------------------------------+ 1 row in set (0.00 sec) mysql> select concat_ws(';','My','s','ql'); +------------------------------+ | concat_ws(';','My','s','ql') | +------------------------------+ | My;s;ql | +------------------------------+ 1 row in set (0.00 sec)
3.REPLACE(str,from_str,to_str)函数的作用:把字符串str中的子字符串from_str全部替换为to_str后得到的字符串,如果to_str为空字符串,那么相当于把from_str子字符串全去掉,如果from_str为空字符串相当于对str不做任何修改,只要输入参数中有null值就返回null。
mysql> select replace('mingyue-s2','-s2',''); +--------------------------------+ | replace('mingyue-s2','-s2','') | +--------------------------------+ | mingyue | +--------------------------------+ 1 row in set (0.00 sec) mysql> select replace('mingyue-s2','-s2',' '); +---------------------------------+ | replace('mingyue-s2','-s2',' ') | +---------------------------------+ | mingyue | +---------------------------------+ 1 row in set (0.00 sec) mysql> select replace('mingyue-s2','-s2',null); +----------------------------------+ | replace('mingyue-s2','-s2',null) | +----------------------------------+ | NULL | +----------------------------------+ 1 row in set (0.00 sec) mysql> select concat('mingyue','-s2'); +-------------------------+ | concat('mingyue','-s2') | +-------------------------+ | mingyue-s2 | +-------------------------+ 1 row in set (0.00 sec) mysql> select replace('mingyue-s2','-s2',' '); +---------------------------------+ | replace('mingyue-s2','-s2',' ') | +---------------------------------+ | mingyue | +---------------------------------+ 1 row in set (0.00 sec)
4.TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)
TRIM([remstr FROM] str)
该函数的作用是在字符串str的首尾去掉子字符串remstr后得到的字符串,有以下情况:
1)如果给出LEADING关键字,函数将去掉str字符串最左端的子字符串remstr
2)如果给出TRAILING关键字,函数将去掉str字符串最右端的子字符串remstr
3)如果给出了BOTH关键字,函数将去掉str字符串最左右两端的子字符串remstr
4)如果没有关键字,默认为BOTH
5)如果没有给出子字符串,默认为空格
mysql> SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx'); +------------------------------------+ | TRIM(LEADING 'x' FROM 'xxxbarxxx') | +------------------------------------+ | barxxx | +------------------------------------+ 1 row in set (0.04 sec) mysql> SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx'); +---------------------------------+ | TRIM(BOTH 'x' FROM 'xxxbarxxx') | +---------------------------------+ | bar | +---------------------------------+ 1 row in set (0.00 sec) mysql> SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz'); +-------------------------------------+ | TRIM(TRAILING 'xyz' FROM 'barxxyz') | +-------------------------------------+ | barx | +-------------------------------------+ 1 row in set (0.00 sec) mysql> select trim(trailing '-s2' from 'mingyue-s2'); +----------------------------------------+ | trim(trailing '-s2' from 'mingyue-s2') | +----------------------------------------+ | mingyue | +----------------------------------------+ 1 row in set (0.00 sec) mysql> select trim('-s2' from 'mingyue-s2'); +-------------------------------+ | trim('-s2' from 'mingyue-s2') | +-------------------------------+ | mingyue | +-------------------------------+ 1 row in set (0.00 sec)
5.REVERSE(str)函数:把字符串倒序排列
mysql> select reverse('mingyue-s2');
+-----------------------+
| reverse('mingyue-s2') |
+-----------------------+
| 2s-euygnim |
+-----------------------+
1 row in set (0.00 sec)
mysql> select reverse('abc');
+----------------+
| reverse('abc') |
+----------------+
| cba |
+----------------+
1 row in set (0.00 sec)
6.LENGTH(str)函数:返回str的长度,以字节为单位
mysql> select length('zhu'); +---------------+ | length('zhu') | +---------------+ | 3 | +---------------+ 1 row in set (0.00 sec) mysql> select length('mingyue-s2'); +----------------------+ | length('mingyue-s2') | +----------------------+ | 10 | +----------------------+ 1 row in set (0.00 sec)
7.LEFT(str,len)函数:返回字符串最左端的len个字符
8.RIGHT(str,len)函数:返回字符串最右端的len个字符
mysql> select left('mingyue-s2',7); +----------------------+ | left('mingyue-s2',7) | +----------------------+ | mingyue | +----------------------+ 1 row in set (0.00 sec) mysql> select right('mingyue-s2',7); +-----------------------+ | right('mingyue-s2',7) | +-----------------------+ | gyue-s2 | +-----------------------+ 1 row in set (0.00 sec)
mysql> select left('mingyue-s2',length('mingyue-s2') - 3);
+---------------------------------------------+
| left('mingyue-s2',length('mingyue-s2') - 3) |
+---------------------------------------------+
| mingyue |
+---------------------------------------------+
1 row in set (0.00 sec)
转载于:https://blog.51cto.com/zhujiangtao/1558088