大数据——MySQL字符串函数

MySQL字符串函数

1、计算字符串字符数char_length(str)和字符串长度的函数length(str)

(1)char_length(str)

【例】使用char_length函数计算字符串字符个数,SQL语句如下:

mysql> select char_length('date'),
    -> char_length('egg');
+---------------------+--------------------+
| char_length('date') | char_length('egg') |
+---------------------+--------------------+
|                   4 |                  3 |
+---------------------+--------------------+
1 row in set (0.06 sec)

(2)length(str)

【例】使用length(str)函数计算字符串长度,SQL语句如下:

mysql> select length('date'),length('egg');
+----------------+---------------+
| length('date') | length('egg') |
+----------------+---------------+
|              4 |             3 |
+----------------+---------------+
1 row in set (0.00 sec)

2、合并字符串长度concat(s1,s2,…)、concat_ws(x,s1,s2,…)。

(1)concat(s1,s2…)

【例】使用concat函数连接字符串,SQL语句如下:

mysql> select concat('My SQL','5.7'),
    -> concat('My',null,'SQL');
+------------------------+-------------------------+
| concat('My SQL','5.7') | concat('My',null,'SQL') |
+------------------------+-------------------------+
| My SQL5.7              | NULL                    |
+------------------------+-------------------------+
1 row in set (0.05 sec)

(2)concat_ws(x,s1,s2,…)

【例】使用concat_ws函数连接带分隔符的字符串,SQL语句如下:

mysql> select concat_ws('-','1st','2nd','3rd'),concat_ws('*','1st',null,'3rd');
+----------------------------------+---------------------------------+
| concat_ws('-','1st','2nd','3rd') | concat_ws('*','1st',null,'3rd') |
+----------------------------------+---------------------------------+
| 1st-2nd-3rd                      | 1st*3rd                         |
+----------------------------------+---------------------------------+
1 row in set (0.05 sec)

3、替换字符串的函数insert(s1,x,len,s2)

insert(s1,x,len,s2)

【例】使用insert函数进行字符串代替操作,SQL语句如下:

mysql> select insert('Quest',2,4,'What') as coll;
+-------+
| coll  |
+-------+
| QWhat |
+-------+
1 row in set (0.05 sec)

4、字母大小写转换函数LOWER(str)、LCASE(str)、UPPER(str)、UCASEE(str)

(1)LOWER(str)和LCASE(str)

【例】使用lower函数或者lcase函数将字符串str中的字母字符全部转换成小写,SQL语句如下:

mysql> select lower('BEAUTIFUL'),lcase('WeLL');
+--------------------+---------------+
| lower('beautiful') | lcase('well') |
+--------------------+---------------+
| beautiful          | well          |
+--------------------+---------------+
1 row in set (0.05 sec)

(2)UPPER(str)和UCASEE(str)

【例】使用upper或者ucase函数将字符串中所有字母字符转换为大写,SQL语句如下:

mysql> select upper('black'),ucase('BLacK');
+----------------+----------------+
| upper('black') | ucase('BLacK') |
+----------------+----------------+
| BLACK          | BLACK          |
+----------------+----------

5、获取指定长度的字符串的函数left(s,n)、right(s,n)

(1)left(s,n)

【例】使用left函数返回字符串中左边的字符,SQL语句如下:

mysql> select left('football',5);
+--------------------+
| left('football',5) |
+--------------------+
| footb              |
+--------------------+
1 row in set (0.00 sec)

(2)right(s,n)

【例】使用right函数返回字符串str最右边n个字符,SQL语句如下:

mysql> select right ('football',4);
+----------------------+
| right ('football',4) |
+----------------------+
| ball                 |
+----------------------+
1 row in set (0.00 sec)

6、填充字符串的函数LPAD(s1,len,s2)、RPAD(s1,len,s2)

(1)LPAD(s1,;en,s2)

【例】使用LPAD函数对字符串进行填充操作,SQL语句如下:

mysql> select lpad('hello',4,'??'),lpad('hello',10,'??');
+----------------------+-----------------------+
| lpad('hello',4,'??') | lpad('hello',10,'??') |
+----------------------+-----------------------+
| hell                 | ?????hello            |
+----------------------+-----------------------+
1 row in set (0.00 sec)

(2)RPAD(s1,len,s2)

【例】使用RPAD函数对字符串进行填充操作,SQL语句如下:

mysql> select rpad('hello',4,'?'),rpad('hello',10,'?');
+---------------------+----------------------+
| rpad('hello',4,'?') | rpad('hello',10,'?') |
+---------------------+----------------------+
| hell                | hello?????           |
+---------------------+----------------------+
1 row in set (0.00 sec)

7、删除空格的函数LTRIM(s)、RTRIM(s)、TRIM(s)

(1)LTRIM(s)

【例】使用LTRIM函数删除字符串左边的空格,SQL语句如下

mysql> select '( book )',
    -> concat('(',ltrim(' book '),')');
+----------+---------------------------------+
| ( book ) | concat('(',ltrim(' book '),')') |
+----------+---------------------------------+
| ( book ) | (book )                         |
+----------+---------------------------------+
1 row in set (0.05 sec)

(2)RTRIM(s)

【例】使用RTRIM函数删除字符串右边的空格,SQL语句如下:

mysql> select '( book )',concat('(',rtrim(' book '),')');
+----------+---------------------------------+
| ( book ) | concat('(',rtrim(' book '),')') |
+----------+---------------------------------+
| ( book ) | ( book)                         |
+----------+---------------------------------+
1 row in set (0.05 sec)

(3)TRIM(s)

【例】使用TRIM函数删除字符串两侧的空格,SQL语句如下:

mysql> select '( book )',concat('(',trim(' book '),')');
+----------+--------------------------------+
| ( book ) | concat('(',trim(' book '),')') |
+----------+--------------------------------+
| ( book ) | (book)                         |
+----------+--------------------------------+
1 row in set (0.05 sec)

8、删除指定字符串的函数TRIM(s1 from s)

TRIM(s1 from s)

【例】使用TRIM(s1 from s)函数删除字符串中两端指定的字符,SQL语句如下:

mysql> select trim('xy' from 'xyxboxyokxxyxy');
+----------------------------------+
| trim('xy' from 'xyxboxyokxxyxy') |
+----------------------------------+
| xboxyokx                         |
+----------------------------------+
1 row in set (0.00 sec)

9、重复生成字符串的函数repeat(s,n)

repeat(s,n)

【例】使用repeat函数重复生成相同的字符串,SQL语句如下:

mysql> select repeat('mysql',3);
+-------------------+
| repeat('mysql',3) |
+-------------------+
| mysqlmysqlmysql   |
+-------------------+
1 row in set (0.00 sec)

10、空格函数SPACE(n)、替换函数REPLA(s,s1,s2)。

(1)space(n)

【例】使用space函数生成由空格组成的字符串,SQL语句如下:

mysql> select concat('(',space(6),')');
+--------------------------+
| concat('(',space(6),')') |
+--------------------------+
| (      )                 |
+--------------------------+
1 row in set (0.00 sec)

(2)replace(s,s1,s2)

【例】使用replace函数进行字符串替代操作,SQL语句如下:

mysql> select replace('xxx.mysql.con','x','w');
+----------------------------------+
| replace('xxx.mysql.con','x','w') |
+----------------------------------+
| www.mysql.con                    |
+----------------------------------+
1 row in set (0.00 sec)

11、比较字符串大小的函数STRCMP(s1,s2)

STRCMP(s1,s2)

若s1和s2相同,返回0;
若s1<s2,返回-1;
若s1>s2,返回1
【例】使用strcmp函数比较字符串大小,SQL语句如下:

mysql> select strcmp('txt','txt2'),
    -> strcmp('txt2','txt'),
    -> strcmp('txt','txt');
+----------------------+----------------------+---------------------+
| strcmp('txt','txt2') | strcmp('txt2','txt') | strcmp('txt','txt') |
+----------------------+----------------------+---------------------+
|                   -1 |                    1 |                   0 |
+----------------------+----------------------+---------------------+
1 row in set (0.00 sec)

12、获取子串的函数SUBSTRING(s,n,len)、MID(s,n,len)

(1)substring(s,n,len)

【例】使用substring函数获取指定位置处的子字符串,SQL语句如下:

mysql> select substring('breakfast',5) as col1,
    -> substring('breakfast',5,3) as col2,
    -> substring('breakfast',-3) as col3,
    -> substring('breakfast',-5,3) as col4;
+-------+------+------+------+
| col1  | col2 | col3 | col4 |
+-------+------+------+------+
| kfast | kfa  | ast  | kfa  |
+-------+------+------+------+
1 row in set (0.00 sec)

(2)mid(s,n,len)

【例】使用mid()函数获取指定位置处的子字符串,SQL语句如下:

mysql> select mid('breakfast',5) as coll,
    -> mid('breakfast',5,3) as col2,
    -> mid('lunch',-3) as col3,
    -> mid('lunch',-5,3) as col4;
+-------+------+------+------+
| coll  | col2 | col3 | col4 |
+-------+------+------+------+
| kfast | kfa  | nch  | lun  |
+-------+------+------+------+
1 row in set (0.00 sec)

13、匹配子串开始位置的函数locate()、position()、instr()

locate()、position()、instr()

【例】使用locate、position、instr函数查找字符串中指定子字符串的开始位置,SQL语句如下:

mysql> select
    -> locate('ball','football'),
    -> position('ball' in 'football'),
    -> instr('football','ball');
+---------------------------+--------------------------------+--------------------------+
| locate('ball','football') | position('ball' in 'football') | instr('football','ball') |
+---------------------------+--------------------------------+--------------------------+
|                         5 |                              5 |                        5 |
+---------------------------+--------------------------------+--------------------------+
1 row in set (0.00 sec)

14、字符串逆序的函数REVERSE(s)

REVERSE(s)

【例】使用reverse函数反转字符串,SQL语句如下:

mysql> select reverse('abc');
+----------------+
| reverse('abc') |
+----------------+
| cba            |
+----------------+
1 row in set (0.00 sec)

15、返回指定位置的字符串的函数elt()

elt()

【例】使用elt函数返回指定位置字符串,SQL语句如下:

mysql> select elt(3,'1st','2nd','3rd'),elt(3,'net','os');
+--------------------------+-------------------+
| elt(3,'1st','2nd','3rd') | elt(3,'net','os') |
+--------------------------+-------------------+
| 3rd                      | NULL              |
+--------------------------+-------------------+
1 row in set (0.00 sec)

16、返回指定字符串位置的函数FIELD(s,s1,s2)

FIELD(s,s1,s2)

返回第一个字符在列表中第一次出现的位置。
【例】使用field函数返回指定字符串第一次出现的位置,SQL语句如下:

mysql> select
    -> field('Hi','hihi','Hey','Hi','bas') as col1,
    -> field('Hi','Hey','Lo','Hilo','foo') as col2;
+------+------+
| col1 | col2 |
+------+------+
|    3 |    0 |
+------+------+
1 row in set (0.00 sec)

17、返回子串位置的函数FIND_IN_SET(S1,S2)

FIND_IN_SET(S1,S2)

find_in_set()函数和field()函数格式不同,但作用类似,都可以返回指定字符串在字符串列表中的位置。
【例】使用find_in_set()函数返回子字符串在字符串列表中的位置,SQL语句如下:

mysql> select find_in_set('Hi','hihi,Hey,Hi,bas');
+-------------------------------------+
| find_in_set('Hi','hihi,Hey,Hi,bas') |
+-------------------------------------+
|                                   3 |
+-------------------------------------+
1 row in set (0.00 sec)

18、选取字符串的函数MAKE_SET(x,s1,s2)

MAKE_SET(x,s1,s2)

【例】使用make_set根据二进制位选取指定字符串,SQL语句如下:

mysql> select make_set(1,'a','b','c') as col1,
    -> make_set(1 |4,'hello','nice','word') as col2,
    -> make_set(1 |4,'hello','nice',null,'world') as col3,
    -> make_set(0,'a','b','c') as col4;
+------+------------+-------+------+
| col1 | col2       | col3  | col4 |
+------+------------+-------+------+
| a    | hello,word | hello |      |
+------+------------+-------+------+
1 row in set (0.00 sec)
注意:
1的二进制是0001,4的二进制是0100,1与4进行或操作之后的二进制为0101,从右到	左第1位和第3位均为1。
make_set(1,‘a’,‘b’,‘c’)返回第1个字符串;make_set(1 | 4,‘hello’,‘nice’,‘world’)返回从左端	开始第1个和第3个字符串组成的字符串。
null不会添加到结果中,因此make_set(1 | 4,‘hello’,‘nice’,null,‘world’)只返回第一个字符	串’hello’。
make_set(0,‘a’,‘b’,‘c’)返回空字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值