mysql 常用字符串函数总结

参考博客:https://www.cnblogs.com/geaozhang/p/6739303.html

名称 功能
lower(column | str) 字符串转小写
upper(column | str) 字符串转大写
concat(column | str, column|str, …) 拼接字符串
concat_ws(separator, column | str, column|str, …), 指定拼接符拼接字符串
substr(str, pos[, length]) 返回字符串的子串,pos索引从1开始,length不指定则到末尾
substring(str, pos[, length]) 同上,索引从1开始
left(str, length) 截取左边指定长度的子串
right(str, length) 截取右边指定长度的子串
length(str) 获取字符串的字节长度
char_length(str) 字符串的字符长度
instr(str, substr) 返回子串在字符串的索引位置,如果没有找到,则返回0
lpad(str, length, padstr) 左填充
rpad(str, length, padstr) } 右填充
trim( [substr from] str ) 去除字符串首尾的指定子串,默认子串为空格
ltrim([substr from] str) 去除字符串左边的指定子串,默认子串为空格
rtrim([substr from] str) 去除字符串右边的指定子串,默认子串为空格
replace(str, from_str, to_str) 替换全部匹配的子串
format(x , D[, local]) 格式化数字x,可用于截取小数点位数,取整等
space(N) 返回由N个空格组成的子串
repeat(str, count) 返回重复字符串count次组成的字符串
reverse(str) 反转字符串
strcmp( str1, str2 ) 比较两个字符串,大于返回1;等于返回0;小于返回-1

1、lower(column | str)

作用:将字符串全部转为小写字母后返回。

select lower('MYSQL Course');
+-----------------------+
| lower('MYSQL_Course') |
+-----------------------+
| mysql_course          |
+-----------------------+

2、upper(column | str)

作用: 将字符串全部转为大写字母后返回。

mysql> select upper('Mysql_Course');
+-----------------------+
| upper('Mysql_Course') |
+-----------------------+
| MYSQL_COURSE          |
+-----------------------+

3、concat(column|str1, column|str2, …)

作用: 拼接字符串.

mysql> select concat('first_name', 'middle_name', 'lasr_name');
+--------------------------------------------------+
| concat('first_name', 'middle_name', 'lasr_name') |
+--------------------------------------------------+
| first_namemiddle_namelasr_name                   |
+--------------------------------------------------+

(1)如果任一一个参数的值为null,则拼接的结果为null。

mysql> select concat('first_name', 'middle_name', 'lasr_name', null);
+--------------------------------------------------------+
| concat('first_name', 'middle_name', 'lasr_name', null) |
+--------------------------------------------------------+
| NULL                                                   |
+--------------------------------------------------------+
1 row in set (0.02 sec)

如果参数有非null非字符串的数据,会自动转换为字符串

mysql> select concat( 1.23, 234, 678 );
+--------------------------+
| concat( 1.23, 234, 678 ) |
+--------------------------+
| 1.23234678               |
+--------------------------+
1 row in set (0.00 sec)

4、concat_ws(separator, str, str2,…)

作用: 按照指定拼接符拼接字符串。如果有参数为null,则忽略该null。

mysql> select concat_ws(',','first_name', 'middle_name', 'lasr_name', null);
+---------------------------------------------------------------+
| concat_ws(',','first_name', 'middle_name', 'lasr_name', null) |
+---------------------------------------------------------------+
| first_name,middle_name,lasr_name                              |
+---------------------------------------------------------------+
1 row in set (0.02 sec)

5、substr( str, pos[, lenght] )
        substring(str, pos[, length])

作用: 截取从指定位置,指定长度的子串。

mysql> select substr('mysql learning', 2,2);
+-------------------------------+
| substr('mysql learning', 2,2) |
+-------------------------------+
| ys                            |
+-------------------------------+

#substring 是 substr的别名函数。
mysql> select substring('mysql learning', 2,2);
+----------------------------------+
| substring('mysql learning', 2,2) |
+----------------------------------+
| ys                               |
+----------------------------------+

5.1、left(str, length)

作用:从左边截取指定长度的子串。

mysql> select left('mysql learning', 2);
+---------------------------+
| left('mysql learning', 2) |
+---------------------------+
| my                        |
+---------------------------+

5.2、 right(str,length)

作用:从右边截取指定长度的子串。

mysql> select right('mysql learning中文', 2);
+----------------------------------+
| right('mysql learning中文', 2)   |
+----------------------------------+
| 中文                             |
+----------------------------------+

6、length(str)

作用: 返回字符串的字节长度

#utf8编码下,
mysql> select length("中文版mysql");
+--------------------------+
| length("中文版mysql")    |
+--------------------------+
|                       14 |
+--------------------------+

7、char_length(str)

作用:返回字符串的字符个数

mysql> select char_length("中文版mysql");
+-------------------------------+
| char_length("中文版mysql")    |
+-------------------------------+
|                             8 |
+-------------------------------+

8、instr(str, substr)

作用:返回子串中首次出现的位置。如果没有找到,则返回0。

mysql> select instr('这是一个基础测试测试','测试');
+--------------------------------------------------+
| instr('这是一个基础测试测试','测试')             |
+--------------------------------------------------+
|                                                7 |
+--------------------------------------------------+

9、lpad(str, length, padstr)

作用:左填充。

mysql> select lpad('first mysql', 20, '####')
    -> ;
+---------------------------------+
| lpad('first mysql', 20, '####') |
+---------------------------------+
| #########first mysql            |
+---------------------------------+

10、rpad(str, length, padstr)

作用:右填充。

mysql> select rpad('first mysql', 20, '####');
+---------------------------------+
| rpad('first mysql', 20, '####') |
+---------------------------------+
| first mysql#########            |
+---------------------------------+

11、trim( [xx from] str )

作用:去除字符串首尾两端的指定子串。默认子串为空格。

mysql> select trim('   hello world  !   ');
+------------------------------+
| trim('   hello world  !   ') |
+------------------------------+
| hello world  !               |
+------------------------------+

#指定要去掉的子串
mysql> select trim(  'a' from  'a a  hello world  !   a a');
+-----------------------------------------------+
| trim(  'a' from  'a a  hello world  !   a a') |
+-----------------------------------------------+
|  a  hello world  !   a                        |

12、ltrim(str)

作用: 去除字符串首端的空格。

mysql> select ltrim( ' a  hello world  !  ');
+--------------------------------+
| ltrim( ' a  hello world  !  ') |
+--------------------------------+
| a  hello world  !              |
+--------------------------------+

13、rtrim([xx from] str)

作用: 去除字符串尾端的空格。

mysql> select rtrim( ' a  hello world  !  ');
+--------------------------------+
| rtrim( ' a  hello world  !  ') |
+--------------------------------+
|  a  hello world  !             |
+--------------------------------+

14、replace(str, from_str, to_str)

作用:替换全部匹配到的子串。

mysql> select replace('这是一个一个一个简单的测试','一','二');
+----------------------------------------------------------------+
| replace('这是一个一个一个简单的测试','一','二')                |
+----------------------------------------------------------------+
| 这是二个二个二个简单的测试                                     |
+----------------------------------------------------------------+

15、format(x, D[,local])

作用:对数字x进行格式化。

mysql> select format(12.3456, 2);
+--------------------+
| format(12.3456, 2) |
+--------------------+
| 12.35              |
+--------------------+

mysql> select format(12.3456, 0);
+--------------------+
| format(12.3456, 0) |
+--------------------+
| 12                 |
+--------------------+

16、reverse(str)

作用:字符串反转。

mysql> select reverse('hello world!');
+-------------------------+
| reverse('hello world!') |
+-------------------------+
| !dlrow olleh            |
+-------------------------+

17、space(count)

作用:返回n个空格组成的子串。

mysql> select concat('a',space(10), 'a');
+----------------------------+
| concat('a',space(10), 'a') |
+----------------------------+
| a          a               |
+----------------------------+

18、repeat(str, count)

作用:返回str重复n次后组成的子串。


mysql> select repeat('a', 10);
+-----------------+
| repeat('a', 10) |
+-----------------+
| aaaaaaaaaa      |
+-----------------+

19、strcmp(str1, str2)

作用:比较两个字符串的大小。如果左边的字符串大于右边的字符串则返回1,相等返回0,小於则返回-1。

mysql> select strcmp('a', 'bb');
+-------------------+
| strcmp('a', 'bb') |
+-------------------+
|                -1 |
+-------------------+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章