当前位置:网站首页>MySQL中的文本處理函數整理,收藏速查
MySQL中的文本處理函數整理,收藏速查
2022-07-04 07:25: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的文本函數就總結到這裏,歡迎大家關注加收藏,同時也歡迎大家光臨翔宇的同名公眾號,翔宇持續更新中!
边栏推荐
- Guoguo took you to write a linked list, and the primary school students said it was good after reading it
- 两年前美国芯片扭捏着不卖芯片,如今芯片堆积如山祈求中国帮忙
- The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
- Status of the thread
- flask-sqlalchemy 循环引用
- 用于压缩视频感知增强的多目标网络自适应时空融合
- Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
- Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help
- com. alibaba. nacos. api. exception. NacosException
- Types of references in BibTex
猜你喜欢

leetcode825. Age appropriate friends

Campus network problems

Status of the thread

提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像

Centos8 install mysql 7 unable to start up

notepad++如何统计单词数量

Distributed transaction management DTM: the little helper behind "buy buy buy"
![SQL foundation 9 [grouping data]](/img/8a/a72941d8c3413316b063033a1b5038.jpg)
SQL foundation 9 [grouping data]

Zephyr study notes 2, scheduling

tornado项目之路由装饰器
随机推荐
jdbc连接es查询的时候,有遇到下面这种情况的大神嘛?
Since DMS is upgraded to a new version, my previous SQL is in the old version of DMS. In this case, how can I retrieve my previous SQL?
Rapidjson reading and writing JSON files
Knowledge payment applet dream vending machine V2
Boast about Devops
What is industrial computer encryption and how to do it
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Status of the thread
Introduction to deep learning Ann neural network parameter optimization problem (SGD, momentum, adagrad, rmsprop, Adam)
notepad++如何统计单词数量
BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
rapidjson读写json文件
[FreeRTOS] FreeRTOS learning notes (7) - handwritten FreeRTOS two-way linked list / source code analysis
A real penetration test
Introduction to spark core components
Design of test cases
Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
The final week, I split
"Sword finger offer" 2nd Edition - force button brush question
Research on an endogenous data security interaction protocol oriented to dual platform and dual chain architecture