首页 > Linux操作系统 > Linux操作系统 > mysql 字符函数小结
挑选了一些日常中常用到的字符函数,整理了一下,供大家参考。
1、 ASCII(str)
返回值为字符串str 的最左字符的数值。假如str为空字符串,则返回值为 0 。假如str 为NULL,则返回值为 NULL。 ASCII()用于带有从 0到255的数值的字符
mysql> select ascii('N');
+------------+
| ascii('N') |
+------------+
| 78 |
+------------+
1 row in set (0.00 sec)
mysql> select ascii('NULL');
+---------------+
| ascii('NULL') |
+---------------+
| 78 |
+---------------+
1 row in set (0.00 sec)
2、 BIN(N)
返回值为N的二进制值的字符串表示,其中 N 为一个longlong (BIGINT) 数字。这等同于 CONV(N,10,2)。假如N 为NULL,则返回值为 NULL
mysql> select bin(2);
+--------+
| bin(2) |
+--------+
| 10 |
+--------+
1 row in set (0.00 sec)
mysql> select bin(3);
+--------+
| bin(3) |
+--------+
| 11 |
+--------+
1 row in set (0.00 sec)
3、 BIT_LENGTH(str)
返回值为二进制的字符串str 长度
mysql> select bit_length('a');
+-----------------+
| bit_length('a') |
+-----------------+
| 8 |
+-----------------+
1 row in set (0.00 sec)
4、 CHAR_LENGTH(str)
返回值为字符串str 的长度,长度的单位为字符。一个多字节字符算作一个单字符。对于一个包含五个二字节字符集, LENGTH()返回值为 10, 而CHAR_LENGTH()的返回值为5。
mysql> select char_length('abcde');
+----------------------+
| char_length('abcde') |
+----------------------+
| 5 |
+----------------------+
1 row in set (0.00 sec)
5、 CONCAT(str1,str2,...)
返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。或许有一个或多个参数。 如果所有参数均为非二进制字符串,则结果为非二进制字符串。
如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如: SELECT CONCAT(CAST(int_col AS CHAR), char_col)
mysql> select concat('My','S','QL');
+-----------------------+
| concat('My','S','QL') |
+-----------------------+
| MySQL |
+-----------------------+
1 row in set (0.00 sec)
mysql> select concat('My','S',null);
+-----------------------+
| concat('My','S',null) |
+-----------------------+
| NULL |
+-----------------------+
1 row in set (0.00 sec)
6、 CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。 第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。
分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。
mysql> select concat_ws(':','what','are','you'), concat_ws(':','what',null,'you'), concat_ws(null,'what','are','you');
+-----------------------------------+----------------------------------+------------------------------------+
| concat_ws(':','what','are','you') | concat_ws(':','what',null,'you') | concat_ws(null,'what','are','you') |
+-----------------------------------+----------------------------------+------------------------------------+
| what:are:you | what:you | NULL |
+-----------------------------------+----------------------------------+------------------------------------+
1 row in set (0.00 sec)
7、 ELT(N,str1,str2,str3,...)
若N = 1,则返回值为 str1 ,若N = 2,则返回值为 str2 ,以此类推。若N 小于1或大于参数的数目,则返回值为 NULL 。 ELT() 是 FIELD()的补数。
mysql> select elt(1,'first','second','three','four'),elt(4,'first','second','three','four');
+----------------------------------------+----------------------------------------+
| elt(1,'first','second','three','four') | elt(4,'first','second','three','four') |
+----------------------------------------+----------------------------------------+
| first | four |
+----------------------------------------+----------------------------------------+
1 row in set (0.00 sec)
8、 HEX(N_or_S)
如果N_OR_S 是一个数字,则返回一个十六进制值 N 的字符串表示,在这里,N 是一个longlong (BIGINT)数。这相当于 CONV(N,10,16)。
如果N_OR_S 是一个字符串,则返回值为一个N_OR_S的十六进制字符串表示, 其中每个N_OR_S 里的每个字符被转化为两个十六进制数字。
mysql> select hex(255),hex('abc');
+----------+------------+
| hex(255) | hex('abc') |
+----------+------------+
| FF | 616263 |
+----------+------------+
1 row in set (0.03 sec)
mysql> select 0x616263;
+----------+
| 0x616263 |
+----------+
| abc |
+----------+
1 row in set (0.00 sec)
9、 INSERT(str,pos,len,newstr)
返回字符串 str, 其子字符串起始于 pos 位置, 长度被字符串 newstr取代的len 字符。
如果pos 超过字符串长度,则返回值为原始字符串。假如len的长度大于其它字符串的长度,则从位置pos开始替换。若任何一个参数为null,则返回值为NULL。
mysql> select insert('abcdefgh',2,4,'what'),insert('abcdefgh',2,100,'what'),insert('abcdefgh',-1,4,'what'),insert('abcdefgh',2,null,'what');
+-------------------------------+---------------------------------+--------------------------------+----------------------------------+
| insert('abcdefgh',2,4,'what') | insert('abcdefgh',2,100,'what') | insert('abcdefgh',-1,4,'what') | insert('abcdefgh',2,null,'what') |
+-------------------------------+---------------------------------+--------------------------------+----------------------------------+
| awhatfgh | awhat | abcdefgh | NULL |
+-------------------------------+---------------------------------+--------------------------------+----------------------------------+
1 row in set (0.00 sec)
10、INSTR(str,substr)
返回字符串 str 中子字符串的第一个出现位置。这和LOCATE()的双参数形式相同,除非参数的顺序被颠倒。
mysql> select instr('abcedefg','ac'),instr('abcedefg','ed');
+------------------------+------------------------+
| instr('abcedefg','ac') | instr('abcedefg','ed') |
+------------------------+------------------------+
| 0 | 4 |
+------------------------+------------------------+
1 row in set (0.00 sec)
11、LEFT(str,len)
返回从字符串str 开始的len 最左字符。
mysql> select left('abcedefg',5);
+--------------------+
| left('abcedefg',5) |
+--------------------+
| abced |
+--------------------+
1 row in set (0.00 sec)
12、RIGHT(str,len)
从字符串str 开始,返回最右len 字符
mysql> select right('abcedefg',5);
+---------------------+
| right('abcedefg',5) |
+---------------------+
| edefg |
+---------------------+
1 row in set (0.00 sec)
13、LOAD_FILE(file_name)
读取文件并将这一文件按照字符串的格式返回。 文件的位置必须在服务器上,你必须为文件制定路径全名,而且你还必须拥有FILE 特许权。
文件必须可读取,文件容量必须小于 max_allowed_packet字节。
mysql> update test set blob_col = load_file('/tmp/picture') where id = 1;
14、LOCATE(substr,str) , LOCATE(substr,str,pos)
第一个语法返回字符串 str中子字符串substr的第一个出现位置。第二个语法返回字符串 str中子字符串substr的第一个出现位置, 起始位置在pos。
如若substr 不在str中,则返回值为0。
mysql> select locate('bar','foobarbar',5),locate('bar','foobarbar');
+-----------------------------+---------------------------+
| locate('bar','foobarbar',5) | locate('bar','foobarbar') |
+-----------------------------+---------------------------+
| 7 | 4 |
+-----------------------------+---------------------------+
1 row in set (0.00 sec)
15、LOWER(str)/ UPPER(str)
将字符串进行大小写转换
mysql> select lower('AbC'),upper('Abc');
+--------------+--------------+
| lower('AbC') | upper('Abc') |
+--------------+--------------+
| abc | ABC |
+--------------+--------------+
1 row in set (0.00 sec)
17、LPAD(str,len,padstr)
返回字符串 str, 其左边由字符串padstr 填补到len 字符长度。假如str 的长度大于len, 则返回值被缩短至 len 字符。
mysql> select lpad('hi',4,'?'),lpad('hi',1,'?');
+------------------+------------------+
| lpad('hi',4,'?') | lpad('hi',1,'?') |
+------------------+------------------+
| ??hi | h |
+------------------+------------------+
1 row in set (0.00 sec)
18、RPAD(str,len,padstr)
返回字符串str, 其右边被字符串 padstr填补至len 字符长度。假如字符串str 的长度大于len,则返回值被缩短到与 len 字符相同长度。
mysql> select rpad('hi',4,'?'),rpad('hi',1,'?');
+------------------+------------------+
| rpad('hi',4,'?') | rpad('hi',1,'?') |
+------------------+------------------+
| hi?? | h |
+------------------+------------------+
1 row in set (0.03 sec)
19、LTRIM(str)/ RTRIM(str)
返回结果字符串,并去除左/右侧的空格
mysql> select ltrim(' xx '),rtrim(' xx ');
+------------------+-----------------+
| ltrim(' xx ') | rtrim(' xx ') |
+------------------+-----------------+
| xx | xx |
+------------------+-----------------+
1 row in set (0.01 sec)
20、QUOTE(str)
引证一个字符串,由此产生一个在SQL语句中可用作完全转义数据值的结果。
返回的字符串由单引号标注,每例都带有单引号 (‘'’)、 反斜线符号 (‘\’)、 ASCII NUL以及前面有反斜线符号的Control-Z 。如果自变量的值为NULL, 则返回不带单引号的单词 “NULL”。
mysql> select quote('Don\'t!');
+------------------+
| quote('Don\'t!') |
+------------------+
| 'Don\'t!' |
+------------------+
1 row in set (0.02 sec)
21、REPEAT(str,count)
返回一个由重复的字符串str 组成的字符串,字符串str的数目等于count 。若 count <= 0,则返回一个空字符串。若str 或 count 为 NULL,则返回 NULL 。
mysql> select repeat('abc',3),repeat('abc',0),repeat('abc',null);
+-----------------+-----------------+--------------------+
| repeat('abc',3) | repeat('abc',0) | repeat('abc',null) |
+-----------------+-----------------+--------------------+
| abcabcabc | | NULL |
+-----------------+-----------------+--------------------+
1 row in set (0.00 sec)
22、REPLACE(str,from_str,to_str)
返回字符串str 以及所有被字符串to_str替代的字符串from_str 。
mysql> select replace('abcdea','a','ww');
+----------------------------+
| replace('abcdea','a','ww') |
+----------------------------+
| wwbcdeww |
+----------------------------+
1 row in set (0.00 sec)
23、REVERSE(str)
返回字符串 str ,顺序和字符顺序相反。
mysql> select reverse('abcdef');
+-------------------+
| reverse('abcdef') |
+-------------------+
| fedcba |
+-------------------+
1 row in set (0.00 sec)
24、SUBSTRING(str,pos,len)
字符串str返回一个长度同len字符相同的子字符串,起始于位置 pos
mysql> select substr('abcedfg',2,4),substr('abcedfg',2),substr('abcedfg',-2,4),substr('abcedfg',-2);
+-----------------------+---------------------+------------------------+----------------------+
| substr('abcedfg',2,4) | substr('abcedfg',2) | substr('abcedfg',-2,4) | substr('abcedfg',-2) |
+-----------------------+---------------------+------------------------+----------------------+
| bced | bcedfg | fg | fg |
+-----------------------+---------------------+------------------------+----------------------+
1 row in set (0.00 sec)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15480802/viewspace-689884/ ,如需转载,请注明出处,否则将追究法律责任。