MySQL 十大常用字符串函数详解
大家好!我是只谈技术不剪发的 Tony 老师。
数据库函数是一种具有某种功能的模块,可以接收零个或多个输入值,并且返回一个输出值。MySQL 为我们提供了许多用于处理和分析数据的系统函数,本文给大家介绍 10 个常用的字符串函数,以及相关的其他函数。
CONCAT()
CONCAT(str1,str2,…))
函数用于返回多个字符串连接之后的字符串,例如:
SELECT CONCAT('MySQL', '字符串', '函数') AS str;str |--------------+MySQL字符串函数|
SELECT CONCAT('MySQL', NULL, '函数') AS str;str|---+ |
SELECT 'MySQL' '字符串' '函数' AS str;str |--------------+MySQL字符串函数|
📝如果启用了 SQL 模式 PIPES_AS_CONCAT,MySQL 逻辑或运算符(||)也可以用于连接字符串,类似于 Oracle 和 PostgreSQL。
除了 CONCAT(str1,str2,…)) 函数之外,CONCAT_WS(separator,str1,str2,…))
函数表示使用指定的分隔符 separator 连接多个字符串,分隔符为 NULL 则返回 NULL。例如:
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|
另外,CHAR_LENGTH(str)
和CHARACTER_LENGTH(str)
函数用于返回字符串的字符长度,也就是字符个数。例如:
SELECT CHAR_LENGTH('MySQL字符串函数') AS len1, CHARACTER_LENGTH('MySQL字符串函数') AS len2;len1|len2|----+----+ 10| 10|
SELECT BIT_LENGTH('MySQL字符串函数') AS len;len|---+160|
SUBSTRING()
SUBSTRING(str,pos)
、SUBSTRING(str FROM pos)
、SUBSTRING(str,pos,len)
以及SUBSTRING(str FROM pos FOR len)
函数都可以用于返回从指定位置 pos 开始的子串,len 表示返回子串的长度;pos 为 0 表示返回空字符串。例如:
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|字符串函数|
LEFT(str,len)
函数返回字符串 str 左侧的 len 个字符,RIGHT(str,len)
函数返回字符串 str 右侧的 len 个字符。例如:
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 |
另外,REPEAT(str,count)
函数用于将字符串 str 复制 count 次并返回结果。例如:
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| |
LOCATE(substr,str,pos)
函数返回子串 substr 在字符串 str 中从位置 pos 开始第一次出现的索引位置,例如:
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|
更多相关文章
- android 中使用TextView实现分段显示不同颜色的字符串
- exp: 修改Android中strings.xml文件, 动态改变数据
- Android(安卓)中数据库查询方法 query() 中的 select
- android中SqLite query中用selectionArgs处理字符传值
- 修改系统Android版本,版本号
- 获取Android系统信息
- android 解析xml字符串
- android 解析xml字符串
- android 根据TextView宽度 自动缩小字体大小 以适应其宽度