当前位置:网站首页>MySQL中的文本处理函数整理,收藏速查
MySQL中的文本处理函数整理,收藏速查
2022-07-04 07:24:00 【Python和数据分析】
大家好,我是翔宇!
前言
今天整理了一下MySQL中中的文本处理函数,当然如果翔宇整理漏掉了麻烦后台留言。不管是在哪一个编程语言,对文本的处理都是及其重要的,因为大家都是知道,日常遇到的数据中,文本都是会占很大一部门的,因此,大佬们就直接把常用到的处理操作进行封装,这样就不需要大家在进行文本处理时还要自己写函数,而且实际上这样还为很多刚入门的小伙伴给予了友好的感觉,比如很多数据分析师
学习SQL只是想要取一下数,压根就不算学习啥视图、存储过程、函数,所以这个时候,如果能够直接进行使用封装好的内置函数,那简直是小伙伴们的福音啊,说了这么多,就是想要说文本处理函数是很重要的,当然,翔宇这里已经整理了一下,你可以收藏一下,用到时候进行速查就行,不嫌麻烦你也可以像翔宇一样进行尝试整理一下。
1.思维导图

2.使用实例
下面的例子不具体查表,直接用select进行测试。
2.1 截取与拼接
left: 左截取,从前面(左边)向后对字符串截取一定长度的子串,语法:left(字符串,截取长度)
select left('滚滚长江东逝水',4); -- 返回“滚滚长江”
right: 右边截取,从后面(右边)向前对字符串截取一定长度的子串,语法:right(字符串,截取长度)
select right('滚滚长江东逝水',4); -- 返回“江东逝水”
substring : 对字符串进行制定位置和长度的截取子串,语法:substring(字符串,从第几个字符开始[,截取长度])
注意:语法中的[]表示此参数可缺省
此函数第三个参数可缺省(不填),缺省时表示截取到最后一个字符
select substring('滚滚长江董事会',3); -- 返回“长江董事会”
select substring('滚滚长江都是水',3,2); -- 返回“长江”
concat:字符串拼接,语法:concat(字符串1[,字符串2,字符串3,…])
此函数的参数可以给一个或者多个,当参数有null时,结果也返回null
select concat('滚滚') -- 返回 “滚滚”
select concat('滚滚',null) --返回 null
select concat('滚滚','长江','东逝','水') -- 返回 “滚滚长江东逝水”
2.2转换
lower:转换为小写,语法:lower(待转换字符串)
select lower('HELLO,WORLD'); -- 返回 “hello,world”
upper :转换为大写,语法:upper(待转换字符串)
select upper('hello,world'); -- 返回 "HELLO,WORLD"
2.3长度
length :返回字符串的长度 ,语法:length(字符串)
select length('滚滚长江东逝水'); -- 返回21(一个汉字占三个英文字符的长度)
select length('hello'); -- 返回5
select length(null) -- 返回null
2.4查找子串位置
locate:子串在原字符串中能找到时返回其在原字符串中的第一个位置 ,语法 Locate(子串,父串)
说明:子串不存在返回0,子串为null返回null
select locate('sql','翔宇在学习mysql还是哪种sql'); -- 返回8,第一个s是第八个字符
select locate('pgsql','翔宇在学习mysql还是哪种sql'); -- 返回0
select locate(null,'翔宇在学习mysql还是哪种sql'); -- 返回null
position :和locate功能一样,写法不同,语法position(substr in str)
select position('sql' in '翔宇在学习mysql还是哪种sql'); -- 返回8
instr:功能和前面两个一样,写法不同,语法:instr(str,substr)
select instr('翔宇在学习mysql还是哪种sql','sql'); -- 返回8
2.5去空格
ltrim:去掉字符串中的前导空格,就是开头有空格就去掉,语法:ltrim(字符串)
select ltrim(' 现在是凌晨2点 '); -- 返回“现在是凌晨2点 ”
rtrim:去掉字符串的尾部空格,语法:rtrim(字符串)
select rtrim(' 现在是凌晨两点 '); -- 返回“ 现在是凌晨两点”
trim:去除字符串中前导和结尾空格,但并不能去除中间的空格 ,语法:trim(字符串)
select trim(' 现在是 凌晨 两点 '); -- 返回 “现在是 凌晨 两点”
2.6填充
lpad:对字符串进行固定长度返回,超过固定长度的截取,低于固定长度的用指定的字符左填充,语法:lpad(字符串,固定长度,需要填充的字符)
低于固定长度时
select lpad('一gao窝里gaogao',20,'*'); -- 返回 "********一gao窝里gaogao"
超过固定长度时
select lpad('法外狂徒张三',4,'^^'); -- 返回“法外狂徒”
rpad:对字符串进行固定长度返回,超过固定长度的截取,低于固定长度的用指定的字符右填充,语法:rpad(字符串,固定长度,需要填充的字符)
低于固定长度时
select rpad('一gao窝里gaogao',20,'*'); -- 返回 "一gao窝里gaogao********"
超过固定长度时
select rpad('法外狂徒张三',4,'^^'); -- 返回“法外狂徒”
2.7替换
replace:将字符串中的子串进行替换成新子串后进行返回,语法:replace(原字符串,待替换的子串,用于替换的子串)
select replace('滚滚长江东逝水','滚','不滚'); -- 返回“不滚不滚长江东逝水”
注意:进行替换时会全部替换,不可设定替换第几个满足条件的子串
2.8 返回发音
soundex:返回字符串的语音表示形式soundex,有助于比较拼写不同但英语发音相似的单词,语法:soundex(字符串)
例如:
如果库中存在一名名为Y.LEE的客户而搜索的时候的输入错误,下面的sql是不会有任何返回结果的。
SELECT CUSTOMER_NAME FROM CUSTOMER WHERE CUSTOMER_NAME = 'Y LEE'
而如果这样写:
SELECT CUSTOMER_NAME FROM CUSTOMER WHERE SOUNDEX(CUSTOMER_NAME) =SOUNDEX('Y LEE')
因为两者发音相似,所以他们的SOUNDEX值匹配,这样就会返回一条数据。
soundex参考于:https://www.cnblogs.com/shuoli/p/8099212.html
好了,sql的文本函数就总结到这里,欢迎大家关注加收藏,同时也欢迎大家光临翔宇的同名公众号,翔宇持续更新中!
边栏推荐
- The difference between synchronized and lock
- 在已經知道錶格列勾選一個顯示一列
- 人生规划(Flag)
- Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown
- Responsive mobile web test questions
- Pangu open source: multi support and promotion, the wave of chip industry
- Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match
- There is no Chinese prompt below when inputting text in win10 Microsoft Pinyin input method
- BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
- MySQL error resolution - error 1261 (01000): row 1 doesn't contain data for all columns
猜你喜欢
![[thread pool]](/img/de/d19897bdcfd65026cfe2a23c41b8c0.jpg)
[thread pool]

Implementation of ZABBIX agent active mode

【Kubernetes系列】Kubernetes 上安装 KubeSphere

the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘

Centos8 install mysql 7 unable to start up

What is industrial computer encryption and how to do it

There is no Chinese prompt below when inputting text in win10 Microsoft Pinyin input method

Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?
![SQL foundation 9 [grouping data]](/img/8a/a72941d8c3413316b063033a1b5038.jpg)
SQL foundation 9 [grouping data]

Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
随机推荐
Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help
Vulhub vulnerability recurrence 77_ zabbix
[FPGA tutorial case 7] design and implementation of counter based on Verilog
MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
How can the old version of commonly used SQL be migrated to the new version?
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
Basic DOS commands
在已經知道錶格列勾選一個顯示一列
Why does the producer / consumer mode wait () use while instead of if (clear and understandable)
The number of patent applications in China has again surpassed that of the United States and Japan, ranking first in the world for 11 consecutive years
Chapter 1 programming problems
If there are two sources in the same job, it will be reported that one of the databases cannot be found. Is there a boss to answer
Adaptive spatiotemporal fusion of multi-target networks for compressed video perception enhancement
Deep profile data leakage prevention scheme
MySQL storage engine
Data double write consistency between redis and MySQL
在所有SwiftUI版本(1.0-4.0)中原生实现Charts图表视图之思路
Rapidjson reading and writing JSON files
SQL foundation 9 [grouping data]
Paddleocr prompt error: can not import AVX core while this file exists: xxx\paddle\fluid\core_ avx