字符串函数
是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用:
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 | +-------------------------------------------------------+
!!也就是函数圆括号里的第一个项目用来指定分隔符
注意:如果有任何参数为null,则函数不返回null,而是直接忽略它
mysql> select concat_ws(',','id',null,'name'); +---------------------------------+ | concat_ws(',','id',null,'name') | +---------------------------------+ | id,name | +---------------------------------+
打开和关闭管道符号“|”的连接功能
PIPES_AS_CONCAT:将“||”视为字符串的连接操作符而非或运算符
|| 管道连接符:
基本格式:
mysql> select 列名1 || 列名2 || 列名3 from 表名;
在mysql中,进行上式连接查询之后,会将查询结果集在一列中显示(字符串连接),列名是‘列名1 || 列名2 || 列名3’;
mysql> select s_no || s_name || s_age -> from student; +-------------------------+ | s_no || s_name || s_age | +-------------------------+ | 1001张三23 | | 1002李四19 | | 1003马五20 | | 1004甲六17 | | 1005乙七22 | +-------------------------+
注意:
①如果不显示结果,是因为sql_mode参数中没有PIPES_AS_CONCAT,只要给sql_mode参数加入PIPES_AS_CONCAT,就可以实现像CONCAT一样的功能;
②如果不给sql_mode参数加入PIPES_AS_CONCAT的话,|| 默认是or的意思,查询结果是一列显示是1。
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 | +-------------------------+-------------------------+
来源:https://www.cnblogs.com/geaozhang/p/6739303.html