您的位置:首页 > 数据库 > MySQL

Mysql函数总结(1)——字符串函数

2013-06-28 17:46 453 查看
1. ASCII(str)

说明:返回str第一个字符的ASCII码;如果str为空,则返回0;如果str为NULL,则返回NULL。

示例:

mysql> select ascii('name');

+---------------+

| ascii('name') |

+---------------+

| 110 |

+---------------+

1 row in set (0.00 sec)

mysql> select ascii('');

+-----------+

| ascii('') |

+-----------+

| 0 |

+-----------+

1 row in set (0.00 sec)

mysql> select ascii(NULL);

+-------------+

| ascii(NULL) |

+-------------+

| NULL |

+-------------+

1 row in set (0.00 sec)

mysql>

2. BIN(N)

说明:返回N的二进制值的字符串标示,N为BIGINT类型。如果N为NULL,则返回NULL。

示例:

mysql> select bin(100);

+----------+

| bin(100) |

+----------+

| 1100100 |

+----------+

1 row in set (0.00 sec)

mysql>

3.BIN_LENGTH(str)

说明:返回字符串str的长度,单位为BIT。

示例:

mysql> select bit_length('name');

+--------------------+

| bit_length('name') |

+--------------------+

| 32 |

+--------------------+

1 row in set (0.00 sec)

mysql>

4.CHAR(N1, N2 ... [USING charset_name])

说明:返回由整数N1,N2,...组成的字符串。

示例:

mysql> SELECT CHAR(77,121,83,81,'76');

+-------------------------+

| CHAR(77,121,83,81,'76') |

+-------------------------+

| MySQL |

+-------------------------+

1 row in set (0.00 sec)

mysql> select char(77, 77.3, 77.6);

+----------------------+

| char(77, 77.3, 77.6) |

+----------------------+

| MMN |

+----------------------+

1 row in set (0.00 sec)

mysql>

5.CHAR_LENGTH(str)

说明:返回字符串str的长度,单位为character。

示例:

mysql> select char_length('name');

+---------------------+

| char_length('name') |

+---------------------+

| 4 |

+---------------------+

1 row in set (0.00 sec)

mysql>

6.CHARACTER_LENGTH(str)

说明:同CHAR_LENGTH(str)

7.CONCAT(str1,str2,...)

说明:字符串拼接。如果其中任何一个字符串为NULL,则返回NULL

示例:

mysql> select concat('Hello', ' ', 'Mysql');

+-------------------------------+

| concat('Hello', ' ', 'Mysql') |

+-------------------------------+

| Hello Mysql |

+-------------------------------+

1 row in set (0.00 sec)

mysql> select concat('Hello', ' ', 'Mysql', NULL);

+-------------------------------------+

| concat('Hello', ' ', 'Mysql', NULL) |

+-------------------------------------+

| NULL |

+-------------------------------------+

1 row in set (0.00 sec)

mysql>

8.CONCAT_WS(separator,str1,str2,...)

说明:字符串拼接。以sepatator作为分隔符。CONCAT_WS意为Concatenate With Separator。如果separator为NULL,则结果为NULL。

示例:

mysql> select concat_ws(' ', 'Hello', 'Mysql');

+----------------------------------+

| concat_ws(' ', 'Hello', 'Mysql') |

+----------------------------------+

| Hello Mysql |

+----------------------------------+

1 row in set (0.00 sec)

mysql>

9. ELT(N,str1,str2,str3,...)

说明:返回str1,str2,str3,...中的第N个字符串。

示例:

mysql> SELECT ELT(1, 'ej', 'Heja', 'hej', 'foo');

+------------------------------------+

| ELT(1, 'ej', 'Heja', 'hej', 'foo') |

+------------------------------------+

| ej |

+------------------------------------+

1 row in set (0.02 sec)

mysql> SELECT ELT(4, 'ej', 'Heja', 'hej', 'foo');

+------------------------------------+

| ELT(4, 'ej', 'Heja', 'hej', 'foo') |

+------------------------------------+

| foo |

+------------------------------------+

1 row in set (0.00 sec)

mysql>

10.EXPORT_SET(bits,on,off[,separator[,number_of_bits]])

说明:从低到高以此检查bits的各个bit位。如果该位为1,则输出on;如果该位为0,则输出off。separator为输出分隔符,默认为逗号。number_of_bits为需要检查的位数,默认为64

示例:

mysql> select export_set(13, 'yes', 'no', ',', 4);

+-------------------------------------+

| export_set(13, 'yes', 'no', ',', 4) |

+-------------------------------------+

| yes,no,yes,yes |

+-------------------------------------+

1 row in set (0.00 sec)

mysql> select export_set(0b1101, 'yes', 'no', ',', 4);

+-----------------------------------------+

| export_set(0b1101, 'yes', 'no', ',', 4) |

+-----------------------------------------+

| yes,no,yes,yes |

+-----------------------------------------+

1 row in set (0.00 sec)

mysql>

11.FIELD(str,str1,str2,str3,...)

说明:返回str在str1,str2,str3,...中的位置。如果没找到str,则返回0

示例:

mysql> select field('a', 'a', 'b', 'c');

+---------------------------+

| field('a', 'a', 'b', 'c') |

+---------------------------+

| 1 |

+---------------------------+

1 row in set (0.00 sec)

mysql> select field('d', 'a', 'b', 'c');

+---------------------------+

| field('d', 'a', 'b', 'c') |

+---------------------------+

| 0 |

+---------------------------+

1 row in set (0.00 sec)

mysql>

12.FORMAT(X,D[,locale])

说明:将X格式化为'#,###,###.##'的字符串,d为保留的小数位数

示例:

mysql> SELECT FORMAT(12332.123456, 4);

+-------------------------+

| FORMAT(12332.123456, 4) |

+-------------------------+

| 12,332.1235 |

+-------------------------+

1 row in set (0.00 sec)

mysql>

13.HEX(str),HEX(N)

说明:

HEX(str)返回对字符串str的十六进制表示

HEX(N)返回对整数N的十六进制字符串表示

mysql> select hex('Mysql'), hex(255);

+--------------+----------+

| hex('Mysql') | hex(255) |

+--------------+----------+

| 4D7973716C | FF |

+--------------+----------+

1 row in set (0.00 sec)

mysql>

14.INSERT(str,pos,len,newstr)

说明:将str中pos开始,长度为len的字符串替换为newstr,返回新字符串。如果pos越界,则返回str;如果len越界,则替换从pos开始的所有字符串。

示例:

mysql> SELECT INSERT('Quadratic', 3, 4, 'What');

+-----------------------------------+

| INSERT('Quadratic', 3, 4, 'What') |

+-----------------------------------+

| QuWhattic |

+-----------------------------------+

1 row in set (0.03 sec)

mysql> SELECT INSERT('Quadratic', -1, 4, 'What');

+------------------------------------+

| INSERT('Quadratic', -1, 4, 'What') |

+------------------------------------+

| Quadratic |

+------------------------------------+

1 row in set (0.06 sec)

mysql> SELECT INSERT('Quadratic', 3, 100, 'What');

+-------------------------------------+

| INSERT('Quadratic', 3, 100, 'What') |

+-------------------------------------+

| QuWhat |

+-------------------------------------+

1 row in set (0.00 sec)

mysql>

15.INSTR(str,substr)

说明:返回子字符串substr在str中的位置。如果不存在,则返回0。

示例:

mysql> SELECT INSTR('foobarbar', 'bar');

+---------------------------+

| INSTR('foobarbar', 'bar') |

+---------------------------+

| 4 |

+---------------------------+

1 row in set (0.08 sec)

mysql> SELECT INSTR('xbar', 'foobar');

+-------------------------+

| INSTR('xbar', 'foobar') |

+-------------------------+

| 0 |

+-------------------------+

1 row in set (0.00 sec)

mysql>

16.LEFT(str,len)

说明:返回str左端的len个字符

示例:

mysql> SELECT LEFT('foobarbar', 5);

+----------------------+

| LEFT('foobarbar', 5) |

+----------------------+

| fooba |

+----------------------+

1 row in set (0.00 sec)

mysql>

17. LENGTH(str)

说明:返回字符串str的长度,以字节为单位。

示例:

mysql> SELECT LENGTH('text');

+----------------+

| LENGTH('text') |

+----------------+

| 4 |

+----------------+

1 row in set (0.00 sec)

mysql>

18. LOCATE(substr,str), LOCATE(substr,str,pos)

说明:类似于INSTR(str,substr),区别是两个字符串的位置相反。pos表示开始查找的位置。如果没有,则返回0

示例:

mysql> SELECT LOCATE('bar', 'foobarbar');

+----------------------------+

| LOCATE('bar', 'foobarbar') |

+----------------------------+

| 4 |

+----------------------------+

1 row in set (0.00 sec)

mysql> SELECT LOCATE('xbar', 'foobar');

+--------------------------+

| LOCATE('xbar', 'foobar') |

+--------------------------+

| 0 |

+--------------------------+

1 row in set (0.00 sec)

mysql> SELECT LOCATE('bar', 'foobarbar', 5);

+-------------------------------+

| LOCATE('bar', 'foobarbar', 5) |

+-------------------------------+

| 7 |

+-------------------------------+

1 row in set (0.00 sec)

mysql>

19.LOWER(str)

说明:返回字符串str的小写

示例:

mysql> SELECT LOWER('QUADRATICALLY');

+------------------------+

| LOWER('QUADRATICALLY') |

+------------------------+

| quadratically |

+------------------------+

1 row in set (0.00 sec)

mysql>

20. LPAD(str,len,padstr)

说明:使用padstr对str左端进行补齐,使其长度为len。如果str长度大于len,则将str截断为len返回。

示例:

mysql> SELECT LPAD('hi',4,'??');

+-------------------+

| LPAD('hi',4,'??') |

+-------------------+

| ??hi |

+-------------------+

1 row in set (0.00 sec)

mysql> SELECT LPAD('hi',1,'??');

+-------------------+

| LPAD('hi',1,'??') |

+-------------------+

| h |

+-------------------+

1 row in set (0.00 sec)

mysql>

21.LTRIM(str)

说明:去掉str左端的空白字符

示例:

mysql> SELECT LTRIM(' barbar');

+------------------+

| LTRIM(' barbar') |

+------------------+

| barbar |

+------------------+

1 row in set (0.02 sec)

mysql>

22.MAKE_SET(bits,str1,str2,...)

说明:根据bits中的位数,使用str1,str2,...来构造字符串集合。

示例:

mysql> SELECT MAKE_SET(1,'a','b','c');

+-------------------------+

| MAKE_SET(1,'a','b','c') |

+-------------------------+

| a |

+-------------------------+

1 row in set (0.02 sec)

mysql> SELECT MAKE_SET(1 | 4,'hello','nice','world');

+----------------------------------------+

| MAKE_SET(1 | 4,'hello','nice','world') |

+----------------------------------------+

| hello,world |

+----------------------------------------+

1 row in set (0.04 sec)

mysql>

23.OCT(N)

说明:返回数字N的八进制字符串表示

示例:

mysql> SELECT OCT(12);

+---------+

| OCT(12) |

+---------+

| 14 |

+---------+

1 row in set (0.00 sec)

mysql>

24. REPEAT(str,count)

说明:将字符串str重复count次返回

示例:

mysql> SELECT REPEAT('MySQL', 3);

+--------------------+

| REPEAT('MySQL', 3) |

+--------------------+

| MySQLMySQLMySQL |

+--------------------+

1 row in set (0.00 sec)

mysql>

25.REPLACE(str,from_str,to_str)

说明:将字符串str中的所有子字符串from_str替换为to_str

示例:

mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');

+-------------------------------------+

| REPLACE('www.mysql.com', 'w', 'Ww') |

+-------------------------------------+

| WwWwWw.mysql.com |

+-------------------------------------+

1 row in set (0.02 sec)

mysql>

26.REVERSE(str)

说明:逆序输出字符串str

示例:

mysql> SELECT REVERSE('abc');

+----------------+

| REVERSE('abc') |

+----------------+

| cba |

+----------------+

1 row in set (0.00 sec)

mysql>

27.RIGHT(str,len)

说明:返回字符串str的右边len个字符

示例:

mysql> SELECT RIGHT('foobarbar', 4);

+-----------------------+

| RIGHT('foobarbar', 4) |

+-----------------------+

| rbar |

+-----------------------+

1 row in set (0.00 sec)

mysql>

28.RPAD(str,len,padstr)

说明:使用padstr对str右端进行补齐,使其长度为len。如果str长度大于len,则将str截断为len返回。

示例:

mysql> SELECT RPAD('hi',5,'?');

+------------------+

| RPAD('hi',5,'?') |

+------------------+

| hi??? |

+------------------+

1 row in set (0.00 sec)

mysql> SELECT RPAD('hi',1,'?');

+------------------+

| RPAD('hi',1,'?') |

+------------------+

| h |

+------------------+

1 row in set (0.00 sec)

mysql>

29.RTRIM(str)

说明:去掉str右端的空白字符

示例:

mysql> SELECT RTRIM('barbar ');

+------------------+

| RTRIM('barbar ') |

+------------------+

| barbar |

+------------------+

1 row in set (0.00 sec)

mysql>

30.SPACE(N)

说明:返回有N个空白字符组成的字符串

示例:

mysql> SELECT SPACE(6);

+----------+

| SPACE(6) |

+----------+

| |

+----------+

1 row in set (0.00 sec)

mysql>

31.SUBSTRING(str,pos), SUBSTRING(str FROM pos),SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len)

说明:返回str中从pos开始,长度为len个字符的子字符串。如果pos为负值,则从右端开始计算

示例:

mysql> SELECT SUBSTRING('Quadratically',5,6);

+--------------------------------+

| SUBSTRING('Quadratically',5,6) |

+--------------------------------+

| ratica |

+--------------------------------+

1 row in set (0.00 sec)

mysql> SELECT SUBSTRING('Sakila', -5, 3);

+----------------------------+

| SUBSTRING('Sakila', -5, 3) |

+----------------------------+

| aki |

+----------------------------+

1 row in set (0.00 sec)

mysql>

32.SUBSTRING_INDEX(str,delim,count)

说明:返回在str中,delim出现count次之前的所有字符串。如果count为负值,则从右端开始查找

示例:

mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);

+------------------------------------------+

| SUBSTRING_INDEX('www.mysql.com', '.', 2) |

+------------------------------------------+

| www.mysql |

+------------------------------------------+

1 row in set (0.00 sec)

mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);

+-------------------------------------------+

| SUBSTRING_INDEX('www.mysql.com', '.', -2) |

+-------------------------------------------+

| mysql.com |

+-------------------------------------------+

1 row in set (0.00 sec)

mysql>

33.TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)

说明:从str中删除指定位置的remstr字符串。如果不指定remstr,则默认为空白字符。如果不指定位置,则默认为BOTH。

示例:

mysql> SELECT TRIM(' bar ');

+---------------+

| TRIM(' bar ') |

+---------------+

| bar |

+---------------+

1 row in set (0.02 sec)

mysql> SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');

+------------------------------------+

| TRIM(LEADING 'x' FROM 'xxxbarxxx') |

+------------------------------------+

| barxxx |

+------------------------------------+

1 row in set (0.02 sec)

mysql> SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');

+---------------------------------+

| TRIM(BOTH 'x' FROM 'xxxbarxxx') |

+---------------------------------+

| bar |

+---------------------------------+

1 row in set (0.00 sec)

mysql> SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');

+-------------------------------------+

| TRIM(TRAILING 'xyz' FROM 'barxxyz') |

+-------------------------------------+

| barx |

+-------------------------------------+

1 row in set (0.02 sec)

mysql>

34. UNHEX(str)

说明:将str作为十六进制字符,返回其对应的字符串

示例:

mysql> SELECT UNHEX('4D7953514C');

+---------------------+

| UNHEX('4D7953514C') |

+---------------------+

| MySQL |

+---------------------+

1 row in set (0.00 sec)

mysql>

35.UPPER(str)

说明:返回字符串str的大写形式

示例:

mysql> SELECT UPPER('Hej');

+--------------+

| UPPER('Hej') |

+--------------+

| HEJ |

+--------------+

1 row in set (0.00 sec)

mysql>

36.STRCMP(expr1,expr2)

说明:字符串比较

示例:

mysql> SELECT STRCMP('text', 'text2');

+-------------------------+

| STRCMP('text', 'text2') |

+-------------------------+

| -1 |

+-------------------------+

1 row in set (0.06 sec)

mysql> SELECT STRCMP('text2', 'text');

+-------------------------+

| STRCMP('text2', 'text') |

+-------------------------+

| 1 |

+-------------------------+

1 row in set (0.00 sec)

mysql> SELECT STRCMP('text', 'text');

+------------------------+

| STRCMP('text', 'text') |

+------------------------+

| 0 |

+------------------------+

1 row in set (0.00 sec)

mysql>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: