MySQL 十大常用字符串函数
大数据DT
共 9353字,需浏览 19分钟
·
2021-07-27 02:31
导读:数据库函数是一种具有某种功能的模块,可以接收零个或多个输入值,并且返回一个输出值。MySQL 为我们提供了许多用于处理和分析数据的系统函数,本文给大家介绍 10 个常用的字符串函数,以及相关的其他函数。
SELECT CONCAT('MySQL', '字符串', '函数') AS str;
str |
--------------+
MySQL字符串函数|
SELECT CONCAT('MySQL', NULL, '函数') AS str;
str|
---+
|
SELECT 'MySQL' '字符串' '函数' AS str;
str |
--------------+
MySQL字符串函数|
SELECT CONCAT_WS('-', 'MySQL', NULL, '字符串') AS str1,
CONCAT_WS(NULL, 'MySQL', '字符串') AS str2;
str1 |str2|
-----------+----+
MySQL-字符串| |
SELECT LOWER('MySQL字符串函数') AS str1, LCASE('MySQL字符串函数') AS str2;
str1 |str2 |
--------------+--------------+
mysql字符串函数|mysql字符串函数|
SELECT LOWER(BINARY 'MySQL字符串函数') AS str1,
LOWER(CONVERT(BINARY 'MySQL字符串函数' USING utf8mb4)) AS str2;
str1 |str2 |
-------------------+--------------+
MySQLå ç¬¦ä¸²å ½æ °|mysql字符串函数|
SELECT UPPER('MySQL字符串函数') AS str1, UCASE('MySQL字符串函数') AS str2;
str1 |str2 |
--------------+--------------+
MYSQL字符串函数|MYSQL字符串函数|
SELECT LENGTH('MySQL字符串函数') AS len1, OCTET_LENGTH('MySQL字符串函数') AS len2;
len1|len2|
----+----+
20| 20|
SELECT CHAR_LENGTH('MySQL字符串函数') AS len1, CHARACTER_LENGTH('MySQL字符串函数') AS len2;
len1|len2|
----+----+
10| 10|
SELECT BIT_LENGTH('MySQL字符串函数') AS len;
len|
---+
160|
SELECT SUBSTRING('MySQL字符串函数', 6) AS str1,
SUBSTRING('MySQL字符串函数' FROM 6) AS str2,
SUBSTRING('MySQL字符串函数', 6, 3) AS str3,
SUBSTRING('MySQL字符串函数' FROM 6 FOR 3) AS str4,
SUBSTRING('MySQL字符串函数', 0) AS str5;
str1 | str2 |str3 |str4 |str5|
---------+---------+-------+------+----+
字符串函数|字符串函数|字符串 |字符串 | |
SELECT SUBSTRING('MySQL字符串函数', -2) AS str1,
SUBSTRING('MySQL字符串函数', -5, 3) AS str2;
str1 |str2 |
------+------+
函数 |字符串 |
SELECT LEFT('MySQL字符串函数',5) AS str1,
RIGHT('MySQL字符串函数',5) AS str2;
str1 |str2 |
-----+---------+
MySQL|字符串函数|
SELECT SUBSTRING_INDEX('张三;李四;王五', ';', 2) AS str1,
SUBSTRING_INDEX('张三;李四;王五', ';', -2) AS str2;
str1 |str2 |
--------+--------+
张三;李四|李四;王五|
SELECT TRIM(' MySQL字符串函数 ') AS str1,
TRIM('-' FROM '--MySQL字符串函数--') AS str2;
str1 |str2 |
--------------+--------------+
MySQL字符串函数|MySQL字符串函数|
SELECT TRIM(LEADING ' ' FROM ' MySQL字符串函数 ') AS str1,
TRIM(TRAILING '-' FROM '--MySQL字符串函数--') AS str2;
str1 |str2 |
----------------+----------------+
MySQL字符串函数 |--MySQL字符串函数|
SELECT LPAD(123, 6, '0') AS str1, LPAD(123, 2, '0') AS str2,
RPAD(123, 6, '0') AS str1, RPAD(123, 2, '0') AS str1;
str1 |str2|str1 |str1|
------+----+------+----+
000123|12 |123000|12 |
SELECT REPEAT('🔥', 5) AS str;
str |
----------+
🔥🔥🔥🔥🔥|
select INSTR('MySQL字符串函数', '字符串') AS index1,
INSTR('MySQL字符串函数', '日期') AS index2,
INSTR('MySQL字符串函数', '') AS index3,
INSTR('MySQL字符串函数', null) AS index4;
index1|index2|index3|index4|
------+------+------+------+
6| 0| 1| |
SELECT LOCATE('S','MySQL Server', 5) AS ind;
ind|
---+
7|
SELECT FIELD('李四', '张三', '李四', '王五') AS ind;
ind|
---+
2|
SELECT FIND_IN_SET('李四', '张三,李四,王五') AS ind;
ind|
---+
2|
SELECT REPLACE('MySQL字符串函数', '字符串', '日期') AS str1,
REPLACE('MySQL字符串函数', '字符串', '') AS str2;
str1 |str2 |
------------+---------+
MySQL日期函数|MySQL函数|
SELECT INSERT('MySQL字符串函数', 6, 3, '日期') AS str;
str |
------------+
MySQL日期函数|
SELECT REVERSE('上海自来水来自海上')='上海自来水来自海上' AS "回文";
回文|
----+
1|
评论