备注:测试数据库版本为MySQL 8.0
这个blog我们来聊聊常见的字符函数
函数名 | 函数用途 |
---|---|
UPPER() | 返回大写的字符 |
LOWER() | 返回小写的字符 |
LTRIM() | 左边去掉空格 |
TRIM() | 去掉空格 |
RTRIM() | 右边去掉空格 |
SPACE() | 返回指定长度的空格 |
CONCAT() | 连接字符串 |
CONCAT_WS() | 指定分隔符连接字符串 |
CHAR_LENGTH() | 返回字符个数 |
LENGTH() | 返回字节个数 |
SUBSTR() | 截取字符 |
SUBSTRING_INDEX() | 截取字符 |
INSTR() | 字符出现位置函数 |
LEFT() | 返回左边N个字符 |
RIGHT() | 返回右边N个字符 |
LPAD() | 在左边填充指定内容 |
RPAD() | 在右边填充指定内容 |
LIKE | 匹配字符函数 |
QUOTE() | 输出转义字符 |
REPEAT() | 重复字符 |
REPLACE() | 替换字符 |
REVERSE() | 反转字符 |
UPPER() 返回大写的字符
LOWER() 返回小写的字符
select UPPER('Abc');
select LOWER('Abc');
mysql> select UPPER('Abc');
+--------------+
| UPPER('Abc') |
+--------------+
| ABC |
+--------------+
1 row in set (0.00 sec)
mysql> select LOWER('Abc');
+--------------+
| LOWER('Abc') |
+--------------+
| abc |
+--------------+
1 row in set (0.00 sec)
LTRIM() 左边去掉空格
TRIM() 去掉空格
RTRIM() 右边去掉空格
SPACE() 返回指定长度的空格
如果中间有空格需要去掉,可以使用replace
-- 去掉左边、两端、右边的空格
select ltrim(' abc def '),trim(' abc def '),rtrim(' abc def ');
-- 将abcd从字符串中剔除
select trim('abcd' from 'abcdefghia');
-- 将a从字符中剔除
select trim('a' from 'abcdefghia');
-- 返回10个空格
select SPACE(10);
mysql> -- 去掉左边、两端、右边的空格
mysql> select ltrim(' abc def '),trim(' abc def '),rtrim(' abc def ');
+--------------------+-------------------+--------------------+
| ltrim(' abc def ') | trim(' abc def ') | rtrim(' abc def ') |
+--------------------+-------------------+--------------------+
| abc def | abc def | abc def |
+--------------------+-------------------+--------------------+
1 row in set (0.00 sec)
mysql> -- 将abcd从字符串中剔除
mysql> select trim('abcd' from 'abcdefghia');
+--------------------------------+
| trim('abcd' from 'abcdefghia') |
+--------------------------------+
| efghia |
+--------------------------------+
1 row in set (0.00 sec)
mysql> -- 将a从字符中剔除
mysql> select trim('a' from 'abcdefghia');
+-----------------------------+
| trim('a' from 'abcdefghia') |
+-----------------------------+
| bcdefghi |
+-----------------------------+
1 row in set (0.00 sec)
mysql> -- 返回10个空格
mysql> select SPACE(10);
+------------+
| SPACE(10) |
+------------+
| |
+------------+
1 row in set (0.00 sec)
CONCAT() 连接字符串
CONCAT_WS() 指定分隔符连接字符串
语法:
CONCAT(str1,str2,…)
CONCAT_WS(separator,str1,str2,…)
-- concat可以带一个或多个参数
select concat('a','b','c');
select concat('a','b','c','d');
-- 如需要使用分隔符连接,比较麻烦
select concat('a','-','b','-','c','-','d');
-- 当遇到空字符的时候也不好处理
select concat('a','-',null,'-','c','-','d');
-- 此时可以使用concat_ws
select concat_ws('-','a','b','c','d');
select concat_ws('-','a',null,'c','d');
mysql> -- concat可以带一个或多个参数
mysql> select concat('a','b','c');
+---------------------+
| concat('a','b','c') |
+---------------------+
| abc |
+---------------------+
1 row in set (0.00 sec)
mysql> select concat('a','b','c','d');
+-------------------------+
| concat('a','b','c','d') |
+-------------------------+
| abcd |
+-------------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 如需要使用分隔符连接,比较麻烦
mysql> select concat('a','-','b','-','c','-','d');
+-------------------------------------+
| concat('a','-','b','-','c','-','d') |
+-------------------------------------+
| a-b-c-d |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> -- 当遇到空字符的时候也不好处理
mysql> select concat('a','-',null,'-','c','-','d');
+--------------------------------------+
| concat('a','-',null,'-','c','-','d') |
+--------------------------------------+
| NULL |
+--------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 此时可以使用concat_ws
mysql> select concat_ws('-','a','b','c','d');
+--------------------------------+
| concat_ws('-','a','b','c','d') |
+--------------------------------+
| a-b-c-d |
+--------------------------------+
1 row in set (0.00 sec)
mysql> select concat_ws('-','a',null,'c','d');
+---------------------------------+
| concat_ws('-','a',null,'c','d') |
+---------------------------------+
| a-c-d |
+---------------------------------+
1 row in set (0.00 sec)
CHAR_LENGTH() 返回字符个数
LENGTH() 返回字节个数
-- 默认字符集是UTF8 1个汉字3个字节
-- 如果字符都是英文,则输出结果一致
select char_length('abc');
select length('abc');
-- 如果字符中包含中文,则输出结果不一致
select length('张三');
select char_length('张三');
mysql> -- 如果字符都是英文,则输出结果一致
mysql> select char_length('abc');
+--------------------+
| char_length('abc') |
+--------------------+
| 3 |
+--------------------+
1 row in set (0.00 sec)
mysql> select length('abc');
+---------------+
| length('abc') |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 如果字符中包含中文,则输出结果不一致
mysql> select length('张三');
+------------------+
| length('张三') |
+------------------+
| 6 |
+------------------+
1 row in set (0.00 sec)
mysql> select char_length('张三');
+-----------------------+
| char_length('张三') |
+-----------------------+
| 2 |
+-----------------------+
1 row in set (0.00 sec)
SUBSTR() 截取字符
SUBSTRING_INDEX() 截取字符
INSTR() 字符出现位置函数
LEFT()
RIGHT()
语法:
SUBSTR(str,pos), SUBSTR(str FROM pos), SUBSTR(str,pos,len), SUBSTR(str FROM pos FOR len)
SUBSTRING_INDEX(str,delim,count)
INSTR(str,substr)
LEFT(str,len)
RIGHT(str,len)
-- 字符截取,从第5位开始截,len没有值,表示截取到最后
select substr('abcdefghi',5);
-- 字符截取,从第5位开始截,截取2个字符
select substr('abcdefghi',5,2);
-- 字符截取,从第-5位开始截,截取2个字符
select substr('abcdefghi',-5,2);
-- '.'第一次出现的地方,保留左边的
select substring_index('www.mysql.com','.',1);
-- '.'最后一次出现的地方,保留右边的
select substring_index('www.mysql.com','.',-1);
-- '.'出现的位置
select instr('www.mysql.com','.');
-- 从左边开始截取5个字符
select left('www.mysql.com',5);
-- 从右边开始截取5个字符
select right('www.mysql.com',5);
mysql> -- 字符截取,从第5位开始截,len没有值,表示截取到最后
mysql> select substr('abcdefghi',5);
+-----------------------+
| substr('abcdefghi',5) |
+-----------------------+
| efghi |
+-----------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 字符截取,从第5位开始截,截取2个字符
mysql> select substr('abcdefghi',5,2);
+-------------------------+
| substr('abcdefghi',5,2) |
+-------------------------+
| ef |
+-------------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 字符截取,从第-5位开始截,截取2个字符
mysql> select substr('abcdefghi',-5,2);
+--------------------------+
| substr('abcdefghi',-5,2) |
+--------------------------+
| ef |
+--------------------------+
1 row in set (0.00 sec)
mysql>
mysql>
mysql>
mysql> -- '.'第一次出现的地方,保留左边的
mysql> select substring_index('www.mysql.com','.',1);
+----------------------------------------+
| substring_index('www.mysql.com','.',1) |
+----------------------------------------+
| www |
+----------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- '.'最后一次出现的地方,保留右边的
mysql> select substring_index('www.mysql.com','.',-1);
+-----------------------------------------+
| substring_index('www.mysql.com','.',-1) |
+-----------------------------------------+
| com |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- '.'出现的位置
mysql> select instr('www.mysql.com','.');
+----------------------------+
| instr('www.mysql.com','.') |
+----------------------------+
| 4 |
+----------------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 从左边开始截取5个字符
mysql> select left('www.mysql.com',5);
+-------------------------+
| left('www.mysql.com',5) |
+-------------------------+
| www.m |
+-------------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 从右边开始截取5个字符
mysql> select right('www.mysql.com',5);
+--------------------------+
| right('www.mysql.com',5) |
+--------------------------+
| l.com |
+--------------------------+
1 row in set (0.00 sec)
LPAD() 在左边填充指定内容
RPAD() 在右边填充指定内容
语法
LPAD(str,len,padstr)
RPAD(str,len,padstr)
SELECT LPAD('hi',4,'??');
SELECT LPAD('hi',1,'??');
SELECT RPAD('hi',4,'??');
SELECT RPAD('hi',1,'??');
mysql> SELECT LPAD('hi',4,'??');
+-------------------+
| LPAD('hi',4,'??') |
+-------------------+
| ??hi |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT LPAD('hi',1,'??');
+-------------------+
| LPAD('hi',1,'??') |
+-------------------+
| h |
+-------------------+
1 row in set (0.00 sec)
mysql>
mysql>
mysql> SELECT RPAD('hi',4,'??');
+-------------------+
| RPAD('hi',4,'??') |
+-------------------+
| hi?? |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT RPAD('hi',1,'??');
+-------------------+
| RPAD('hi',1,'??') |
+-------------------+
| h |
+-------------------+
1 row in set (0.00 sec)
LIKE 匹配字符函数
QUOTE() 屏蔽字符串中的转移字符
REPEAT() 重复字符
REPLACE() 替换字符
REVERSE() 反转字符
语法:
QUOTE(str)
REPEAT(str,count)
REPLACE(str,from_str,to_str)
REVERSE(str)
-- 测试数据
create table t(name varchar(20));
insert into t values ('abc'),('ABC'),('def'),('abcdef'),('aaa');
-- 匹配a开头的,默认的情况下不区分大小写
select * from t where name like 'a%';
-- 匹配a结尾的
select * from t where name like '%a%';
mysql> -- 匹配a开头的,默认的情况下不区分大小写
mysql> select * from t where name like 'a%';
+--------+
| name |
+--------+
| abc |
| ABC |
| abcdef |
| aaa |
+--------+
4 rows in set (0.00 sec)
mysql>
mysql> -- 匹配a结尾的
mysql> select * from t where name like '%a%';
+--------+
| name |
+--------+
| abc |
| ABC |
| abcdef |
| aaa |
+--------+
4 rows in set (0.00 sec)
-- quote输出完整的支付
select 'abc\.def' as col1,quote('abc\.def') as col2;
-- 重复字符3吃
SELECT REPEAT('MySQL', 3);
-- 将w替换为Ww
SELECT REPLACE('www.mysql.com', 'w', 'Ww');
-- 反转支付
SELECT REVERSE('abc');
mysql> -- quote输出完整的支付
mysql> select 'abc\.def' as col1,quote('abc\.def') as col2;
+---------+-----------+
| col1 | col2 |
+---------+-----------+
| abc.def | 'abc.def' |
+---------+-----------+
1 row in set (0.00 sec)
mysql>
mysql> -- 重复字符3吃
mysql> SELECT REPEAT('MySQL', 3);
+--------------------+
| REPEAT('MySQL', 3) |
+--------------------+
| MySQLMySQLMySQL |
+--------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 将w替换为Ww
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
+-------------------------------------+
| REPLACE('www.mysql.com', 'w', 'Ww') |
+-------------------------------------+
| WwWwWw.mysql.com |
+-------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- 反转支付
mysql> SELECT REVERSE('abc');
+----------------+
| REVERSE('abc') |
+----------------+
| cba |
+----------------+
1 row in set (0.00 sec)
mysql>