| 函数/运算符 | 描述 |
|---|---|
| ASCII() | 返回第一个字符的 ASCII 码 |
| BIT_LENGTH() | 返回以比特为单位的字符串长度 |
| CHAR() | 返回每个编码值对应的字符 |
|
CHAR_LENGTH() CHARACTER_LENGTH() |
返回以字符为单位的字符串长度 |
| CONCAT() | 连接多个字符串 |
| CONCAT_WS() | 使用指定分隔符连接字符串 |
| ELT() | 返回字符串列表中的第 N 个元素 |
| FIELD() | 返回第一个字符串在后续参数列表中的索引位置 |
| FIND_IN_SET() | 返回第一个字符串在第二个字符串列表参数中的索引位置 |
| FROM_BASE64() | 对 base64 编码字符串进行解码并返回结果 |
| INSERT() | 在指定位置插入并替换子串 |
| INSTR() | 返回子串第一次出现的索引位置 |
|
LCASE() LOWER() |
返回小写形式的字符串 |
| LEFT() | 返回字符串左侧的 N 个字符 |
|
LENGTH() OCTET_LENGTH() |
返回以字节为单位的字符串长度 |
|
LOCATE() POSITION() |
返回子串第一次出现的索引位置 |
| LPAD() | 在字符串的左侧进行填充 |
| LTRIM() | 删除字符串左侧的空格 |
| ORD() | 返回第一个字符的编码 |
| REPEAT() | 将字符串重复 N 次 |
| REPLACE() | 替换子串 |
| REVERSE() | 反转字符串 |
| RIGHT() | 返回字符串右侧的 N 个字符 |
| RPAD() | 在字符串的右侧进行填充 |
| RTRIM() | 删除字符串右侧的空格 |
| SPACE() | 返回一个由空格组成的字符串 |
| STRCMP() | 比较两个字符串 |
|
MID() SUBSTR() SUBSTRING() |
返回从指定位置开始的子串 |
| SUBSTRING_INDEX() | 返回指定分隔符之前的子串 |
| TO_BASE64() | 将字符串进行 base-64 编码 |
| TRIM() | 删除字符串两侧的空格 |
|
UCASE() UPPER() |
返回大写形式的字符串 |
select ascii('ABC'), ascii(''), ascii(null);
ascii('ABC')|ascii('')|ascii(null)|
------------|---------|-----------|
65| 0| |
(1st byte code)
+ (2nd byte code * 256)
+ (3rd byte code * 256^2) ...
select ord('ABC'), ord('你好');
ord('ABC')|ord('你好')|
----------|-----------|
65| 14990752|
select char(77, 121, 83, 81, '76');
char(77, 121, 83, 81, '76')|
---------------------------|
MySQL |
select charset(char(65)), charset(char(65 using utf8mb4));
charset(char(65))|charset(char(65 using utf8mb4))|
-----------------|-------------------------------|
binary |utf8mb4 |
select bit_length('你'), char_length('你'), length('你');
bit_length('你')|char_length('你')|length('你')|
---------------|----------------|-----------|
24| 1| 3|
select concat('My', 'SQL', 8.0), concat('My', null, 'SQL');
concat('My', 'SQL', 8.0)|concat('My', null, 'SQL')|
------------------------|-------------------------|
MySQL8.0 | |
select 'My' 'SQL' '8.0';
select concat_ws('-', 'My', null, 'SQL'), concat_ws(null, 'My', 'SQL');
concat_ws('-', 'My', null, 'SQL')|concat_ws(null, 'My', 'SQL')|
---------------------------------|----------------------------|
My-SQL | |
select lower('MySQL'), lcase('MySQL');
lower('MySQL')|lcase('MySQL')|
--------------|--------------|
mysql |mysql |
select upper('MySQL'), ucase('MySQL');
upper('MySQL')|ucase('MySQL')|
--------------|--------------|
MYSQL |MYSQL |
select lower(binary 'MySQL'), lower(CONVERT(binary 'MySQL' USING utf8mb4));
lower(binary 'MySQL')|lower(CONVERT(binary 'MySQL' USING utf8mb4))|
---------------------|--------------------------------------------|
MySQL |mysql |
select instr('MySQL', 'SQL'), instr('Oracle', 'SQL'), instr('MySQL', ''), instr('MySQL', null);
instr('MySQL', 'SQL')|instr('Oracle', 'SQL')|instr('MySQL', '')|instr('MySQL', null)|
---------------------|----------------------|------------------|--------------------|
3| 0| 1| |
select locate('SQL', 'MySQL'), locate('SQL', 'MySQL vs PostgreSQL', 6);
locate('SQL', 'MySQL')|locate('SQL', 'MySQL vs PostgreSQL', 6)|
----------------------|---------------------------------------|
3| 17|
select field('Friday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday') as dow;
dow|
---|
5|
select find_in_set('Friday', 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday') as dow;
dow|
---|
5|
select substring('MySQL', 3), substring('MySQL' from 3 for 3), substring('MySQL', 0);
substring('MySQL', 3)|substring('MySQL' from 3 for 3)|substring('MySQL', 0)|
---------------------|-------------------------------|---------------------|
SQL |SQL | |
select substring('MySQL', -5, 2), substring('MySQL' from -3);
substring('MySQL', -5, 2)|substring('MySQL' from -3)|
-------------------------|--------------------------|
My |SQL |
select substr('MySQL', 3), mid('MySQL' from 3 for 3);
substr('MySQL', 3)|mid('MySQL' from 3 for 3)|
------------------|-------------------------|
SQL |SQL |
select substring_index('', '.', 2), substring_index('', '.', -2);
substring_index('', '.', 2)|substring_index('', '.', -2)|
----------------------------------------|-----------------------------------------|
www.mysql |mysql.com |
select left('MySQL', 2), right('MySQL', 3);
left('MySQL', 2)|right('MySQL', 3)|
----------------|-----------------|
My |SQL |
select ltrim(' my sql ') as lt, rtrim(' my sql ') as rt;
lt |rt |
--------|--------|
my sql | my sql|
select trim(' my sql ') as tr, trim(both '1' from '111mysql111') as tr;
tr |tr |
------|-----|
my sql|mysql|
select elt(7, 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday') as dow;
dow |
------|
Sunday|
select replace('www.mysql.net', 'net', 'com'), replace('mysql', 'my', '');
replace('www.mysql.net', 'net', 'com')|replace('mysql', 'my', '')|
--------------------------------------|--------------------------|
www.mysql.com |sql |
select insert('mysql.com', 7, 3, 'cn'), insert('mysql.com', 10, 3, 'cn');
insert('mysql.com', 7, 3, 'cn')|insert('mysql.com', 10, 3, 'cn')|
-------------------------------|--------------------------------|
mysql.cn |mysql.com |
select reverse('123');
reverse('123')|
--------------|
321 |
select lpad('MySQL 8.0', 12,'-') as lpad, lpad('MySQL 8.0', 5,'-') as lpad,
rpad('MySQL 8.0', 12,'-') as rpad, rpad('MySQL 8.0', 5,'-') as rpad;
lpad |lpad |rpad |rpad |
------------|-----|------------|-----|
---MySQL 8.0|MySQL|MySQL 8.0---|MySQL|
select repeat('🔥', 3);
repeat('🔥', 3)|
--------------|
🔥🔥🔥 |
select concat('my', space(5), 'sql') as space;
space |
----------|
my sql|
select strcmp('mysql', 'MySQL') as strcmp, strcmp('mysql' collate utf8mb4_0900_as_cs, 'MySQL') as strcmp;
strcmp|strcmp|
------|------|
0| -1|
select 'mysql' = 'MySQL', 'mysql' != 'MySQL' collate utf8mb4_0900_as_cs as cmp;
'mysql' = 'MySQL'|cmp|
-----------------|---|
1| 1|
select to_base64('MySQL'), from_base64('TXlTUUw=');
to_base64('MySQL')|from_base64('TXlTUUw=')|
------------------|-----------------------|
TXlTUUw= |MySQL |
原文作者:SQL编程思想;如有侵权,请联系管理员删除。