上一节我们介绍了日期函数,这一节我们来看看有关于字符串的函数。
SQL里字符串的类型是char和varchar,本节所有的字符串函数都是围绕着这两个数据类型展开的,对于这两个数据类型有疑问可以参考:
常用的字符串函数有下面几种:
1. charset(str)
charset()函数返回传入参数的字符集,参数可以是一个自定义的字符串也可以是一个表的字段名,通常应用场景为后者,使用如下:
mysql> select charset('test');
+-----------------+
| charset('test') |
+-----------------+
| gbk |
+-----------------+
1 row in set (0.01 sec)
这是在cmd控制台中返回的自定义字符串结果,这是因为Windows cmd控制台字符集采用的是gbk,如果在MySQL自带的Client中使用相同的语句,将会返回utf8。
2. concat(str1, str2, …)
concat()函数会将传入的字符创全部拼接起来并返回,使用如下:
mysql> select concat('hello', ' world');
+---------------------------+
| concat('hello', ' world') |
+---------------------------+
| hello world |
+---------------------------+
1 row in set (0.00 sec)
concat()函数一般用于输出结果字符串的拼接,使显示更为清晰化。
3. instr(str, substr)
instr()函数用于显示substr子串在str字符串中第一次出现的下标位置,使用如下:
mysql> select instr('abcccccc', 'c');
+------------------------+
| instr('abcccccc', 'c') |
+------------------------+
| 3 |
+------------------------+
1 row in set (0.01 sec)
这里要注意,MySQL中所有字符串的小标从1开始,所以这里返回的是3而不是2。
4. ucase(str)/lcase(str)
这两个函数很好理解,就是将字符串str返回为全部大写和全部小写,使用如下:
-- 全部转为大写
mysql> select ucase('mysql');
+----------------+
| ucase('mysql') |
+----------------+
| MYSQL |
+----------------+
1 row in set (0.00 sec)
-- 全部转为小写
mysql> select lcase('MYSQL');
+----------------+
| lcase('MYSQL') |
+----------------+
| mysql |
+----------------+
1 row in set (0.00 sec)
5. left(str, len)
left()函数功能比较抽象,实际上就是从str字符串左侧(即字符串首)开始取len个字符,并将其返回,使用如下:
mysql> select left('hello world', '5');
+--------------------------+
| left('hello world', '5') |
+--------------------------+
| hello |
+--------------------------+
1 row in set (0.00 sec)
6. length(str)
length()函数返回传入字符串的字节的长度,注意,是字节的长度,不是字符串的长度!!!使用如下:
mysql> select length('hello');
+-----------------+
| length('hello') |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.03 sec)
mysql> select length('你好');
+----------------+
| length('你好') |
+----------------+
| 4 |
+----------------+
1 row in set (0.00 sec)
上面的例子中,当参数为字母时,因为字符串的字符集为gbk,所以一个字母一个字节,而传入中文结果就不一样了,因为在gbk中一个中文占两个字节。如果实在utf8字符集中,”你好”返回的结果就是6了,因为utf8字符集中一个中文占3个字节。
7. replace(str, search_str, replace_str)
将字符串str中所有的search_str子串全部替换为replace_str子串,使用如下:
-- 将字符串中所有world替换为China
mysql> select replace('hello world, world', 'world', 'China');
+-------------------------------------------------+
| replace('hello world, world', 'world', 'China') |
+-------------------------------------------------+
| hello China, China |
+-------------------------------------------------+
1 row in set (0.00 sec)
8. substring(str, postion[, len])
substring()函数从字符串str中取指定位置的子串,也可以指定子串的长度,如果不指定,子串就与字符串一起结束,使用如下:
mysql> select substring('hello world', 2);
+-----------------------------+
| substring('hello world', 2) |
+-----------------------------+
| ello world |
+-----------------------------+
1 row in set (0.00 sec)
mysql> select substring('hello world', 2, 4);
+--------------------------------+
| substring('hello world', 2, 4) |
+--------------------------------+
| ello |
+--------------------------------+
1 row in set (0.00 sec)
和上面instr()要注意的一样,MySQL字符串下标是从1开始算的。
9. ltrim(str)/rtrim(str)/trim(str)
java的String类中也有trim()这个函数,用来去除字符串中的前后空格,数据库将去除空格分为三个函数,顾名思义,ltrim()去除字符串起始处的空格,rtrim()去除字符串结尾处的空格,trim()去除左右两侧所有的空格,使用如下:
mysql> select ltrim(' a b ') as result;
+---------------------------+
| result |
+---------------------------+
| a b |
+---------------------------+
1 row in set (0.00 sec)
mysql> select rtrim(' a b ') as result;
+--------------------------+
| result |
+--------------------------+
| a b |
+--------------------------+
1 row in set (0.00 sec)
mysql> select trim(' a b ') as result;
+--------+
| result |
+--------+
| a b |
+--------+
1 row in set (0.00 sec)