欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

mysql字符串函数有哪些?

程序员文章站 2022-03-16 09:23:13
...

mysql字符串函数有:1、LOWER,将字符串参数值转换为全小写字母后返回;2、UPPER,将字符串参数值转换为全大写字母后返回;3、CONCAT,将多个字符串参数首尾相连后返回;4、SUBSTR,从源字符串str中指定位置pos开始取。

mysql字符串函数有哪些?

mysql字符串函数有:

mysql字符串函数有哪些?

1、LOWER(column|str):将字符串参数值转换为全小写字母后返回

mysql> select lower('SQL Course');+---------------------+
| lower('SQL Course') |
+---------------------+
| sql course          |
+---------------------+

2、UPPER(column|str):将字符串参数值转换为全大写字母后返回

mysql> select upper('Use MYsql');+--------------------+
| upper('Use MYsql') |
+--------------------+
| USE MYSQL          |
+--------------------+

3、CONCAT(column|str1, column|str2,...):将多个字符串参数首尾相连后返回

mysql> select concat('My','S','QL');+-----------------------+
| concat('My','S','QL') |
+-----------------------+
| MySQL                 |
+-----------------------+

如果有任何参数为null,则函数返回null

mysql> select concat('My',null,'QL');+------------------------+
| concat('My',null,'QL') |
+------------------------+
| NULL                   |
+------------------------+

如果参数是数字,则自动转换为字符串

mysql> select concat(14.3,'mysql');+----------------------+
| concat(14.3,'mysql') |
+----------------------+
| 14.3mysql            |
+----------------------+

4、CONCAT_WS(separator,str1,str2,...):将多个字符串参数以给定的分隔符separator首尾相连后返回

mysql> select concat_ws(';','First name','Second name','Last name');+-------------------------------------------------------+
| concat_ws(';','First name','Second name','Last name') |
+-------------------------------------------------------+
| First name;Second name;Last name                      |
+-------------------------------------------------------+

!!也就是函数圆括号里的第一个项目用来指定分隔符

5、SUBSTR(str,pos[,len]):从源字符串str中的指定位置pos开始取一个字串并返回

注意:

  ①len指定子串的长度,如果省略则一直取到字符串的末尾;len为负值表示从源字符串的尾部开始取起。

  ②函数SUBSTR()是函数SUBSTRING()的同义词。

mysql> select substring('hello world',5);+----------------------------+
| substring('hello world',5) |
+----------------------------+
| o world                    |
+----------------------------+mysql> select substr('hello world',5,3);+---------------------------+
| substr('hello world',5,3) |
+---------------------------+
| o w                       |
+---------------------------+mysql> select substr('hello world',-5);+--------------------------+
| substr('hello world',-5) |
+--------------------------+
| world                    |
+--------------------------+

6、LENGTH(str):返回字符串的存储长度

mysql> select length('text'),length('你好');+----------------+------------------+
| length('text') | length('你好')   |
+----------------+------------------+
|              4 |                6 |
+----------------+------------------+

注意:编码方式不同字符串的存储长度就不一样(‘你好’:utf8是6,gbk是4)

7、CHAR_LENGTH(str):返回字符串中的字符个数

mysql> select char_length('text'),char_length('你好');+---------------------+-----------------------+
| char_length('text') | char_length('你好')   |
+---------------------+-----------------------+
|                   4 |                     2 |
+---------------------+-----------------------+

8、INSTR(str, substr):从源字符串str中返回子串substr第一次出现的位置

mysql> select instr('foobarbar','bar');+--------------------------+
| instr('foobarbar','bar') |
+--------------------------+
|                        4 |
+--------------------------+

9、LPAD(str, len, padstr):在源字符串的左边填充给定的字符padstr到指定的长度len,返回填充后的字符串

mysql> select lpad('hi',5,'??');+-------------------+
| lpad('hi',5,'??') |
+-------------------+
| ???hi             |
+-------------------+

10、RPAD(str, len, padstr):在源字符串的右边填充给定的字符padstr到指定的长度len,返回填充后的字符串

mysql> select rpad('hi',6,'??');+-------------------+| rpad('hi',6,'??') |+-------------------+| hi????            |+-------------------+


11、TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str):

从源字符串str中去掉两端、前缀或后缀字符remstr并返回;

  如果不指定remstr,则去掉str两端的空格;不指定BOTH、LEADING、TRAILING ,则默认为 BOTH。

mysql> select trim('  bar  ');+-----------------+
| trim('  bar  ') |
+-----------------+
| bar             |
+-----------------+mysql> select trim(leading 'x' from 'xxxbarxxx');+------------------------------------+
| trim(leading 'x' from 'xxxbarxxx') |
+------------------------------------+
| barxxx                             |
+------------------------------------+mysql> select trim(both 'x' from 'xxxbarxxx');+---------------------------------+
| trim(both 'x' from 'xxxbarxxx') |
+---------------------------------+
| bar                             |
+---------------------------------+mysql> select trim(trailing 'xyz' from 'barxxyz');+-------------------------------------+
| trim(trailing 'xyz' from 'barxxyz') |
+-------------------------------------+
| barx                                |
+-------------------------------------+

12、REPLACE(str, from_str, to_str):在源字符串str中查找所有的子串form_str(大小写敏感),找到后使用替代字符串to_str替换它。返回替换后的字符串

mysql> select replace('www.mysql.com','w','Ww');+-----------------------------------+
| replace('www.mysql.com','w','Ww') |
+-----------------------------------+
| WwWwWw.mysql.com                  |
+-----------------------------------+

13、LTRIM(str),RTRIM(str):去掉字符串的左边或右边的空格(左对齐、右对齐)

mysql> SELECT  ltrim('   barbar   ') rs1, rtrim('   barbar   ') rs2;+-----------+-----------+
| rs1       | rs2       |
+-----------+-----------+
| barbar    |    barbar |
+-----------+-----------+

14、REPEAT(str, count):将字符串str重复count次后返回

mysql> select repeat('MySQL',3);+-------------------+
| repeat('MySQL',3) |
+-------------------+
| MySQLMySQLMySQL   |
+-------------------+

15、REVERSE(str):将字符串str反转后返回

mysql> select reverse('abcdef');+-------------------+
| reverse('abcdef') |
+-------------------+
| fedcba            |
+-------------------+

16、CHAR(N,... [USING charset_name]):将每个参数N解释为整数(字符的编码),并返回每个整数对应的字符所构成的字符串(NULL值被忽略)。

mysql> select char(77,121,83,81,'76'),char(77,77.3,'77.3');+-------------------------+----------------------+
| char(77,121,83,81,'76') | char(77,77.3,'77.3') |
+-------------------------+----------------------+
| MySQL                   | MMM                  |
+-------------------------+----------------------+

默认情况下,函数返回二进制字符串,若想返回针对特定字符集的字符串,使用using选项

mysql> SELECT charset(char(0x65)), charset(char(0x65 USING utf8));+---------------------+--------------------------------+
| charset(char(0x65)) | charset(char(0x65 USING utf8)) |
+---------------------+--------------------------------+
| binary              | utf8                           |
+---------------------+--------------------------------+

17、FORMAT(X,D[,locale]):以格式‘#,###,###.##’格式化数字X

  • D指定小数位数

  • locale指定国家语言(默认的locale为en_US)

mysql> SELECT format(12332.123456, 4),format(12332.2,0);+-------------------------+-------------------+
| format(12332.123456, 4) | format(12332.2,0) |
+-------------------------+-------------------+
| 12,332.1235 | 12,332 |
+-------------------------+-------------------+mysql> SELECT format(12332.2,2,'de_DE');+---------------------------+
| format(12332.2,2,'de_DE') |
+---------------------------+
| 12.332,20 |
+---------------------------+

18、SPACE(N):返回由N个空格构成的字符串

mysql> select space(3);+----------+
| space(3) |
+----------+
|          |
+----------+

19、LEFT(str, len):返回最左边的len长度的子串

mysql> select left('chinaitsoft',5);+-----------------------+
| left('chinaitsoft',5) |
+-----------------------+
| china                 |
+-----------------------+

20、RIGHT(str, len):返回最右边的len长度的子串

mysql> select right('chinaitsoft',5);+------------------------+
| right('chinaitsoft',5) |
+------------------------+
| tsoft                  |
+------------------------+

21、STRCMP(expr1,expr2):如果两个字符串是一样的则返回0;如果第一个小于第二个则返回-1;否则返回1

mysql> select strcmp('text','text');+-----------------------+
| strcmp('text','text') |
+-----------------------+
|                     0 |
+-----------------------+mysql> SELECT strcmp('text', 'text2'),strcmp('text2', 'text');+-------------------------+-------------------------+
| strcmp('text', 'text2') | strcmp('text2', 'text') |
+-------------------------+-------------------------+
|                      -1 |                       1 |
+-------------------------+-------------------------+

相关学习推荐:mysql视频教程

以上就是mysql字符串函数有哪些?的详细内容,更多请关注其它相关文章!