当前位置:网站首页>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的文本函数就总结到这里,欢迎大家关注加收藏,同时也欢迎大家光临翔宇的同名公众号,翔宇持续更新中!
边栏推荐
- [untitled] notice on holding "2022 traditional fermented food and modern brewing technology"
- The most effective futures trend strategy: futures reverse merchandising
- jdbc连接es查询的时候,有遇到下面这种情况的大神嘛?
- [FPGA tutorial case 7] design and implementation of counter based on Verilog
- Vulhub vulnerability recurrence 76_ XXL-JOB
- Boast about Devops
- 用于压缩视频感知增强的多目标网络自适应时空融合
- [Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)
- A real penetration test
- Basic DOS commands
猜你喜欢
Handwritten easy version flexible JS and source code analysis
Vulhub vulnerability recurrence 77_ zabbix
大学阶段总结
用于压缩视频感知增强的多目标网络自适应时空融合
The most effective futures trend strategy: futures reverse merchandising
Su Weijie, a member of Qingyuan Association and an assistant professor at the University of Pennsylvania, won the first Siam Youth Award for data science, focusing on privacy data protection, etc
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
Campus network problems
It's healthy to drink medicinal wine like this. Are you drinking it right
随机推荐
JS common time processing functions
How notepad++ counts words
Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
tornado项目之路由装饰器
Introduction to deep learning Ann neural network parameter optimization problem (SGD, momentum, adagrad, rmsprop, Adam)
The final week, I split
2022 - 021arts: début du deuxième semestre
Computer connects raspberry pie remotely through putty
[thread pool]
人生规划(Flag)
Enter the year, month, and determine the number of days
[web security] nodejs prototype chain pollution analysis
Vulhub vulnerability recurrence 76_ XXL-JOB
Mobile adaptation: vw/vh
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
[Chongqing Guangdong education] National Open University spring 2019 770 real estate appraisal reference questions
电脑通过Putty远程连接树莓派
Set JTAG fuc invalid to normal IO port
Centos8 install mysql 7 unable to start up
[real case] how to deal with the failure of message consumption?