当前位置:网站首页>How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
2022-07-31 10:18:00 【xiaoweiwei99】
Table of Contents
1.LEFT function
(1) Grammar: left(text,len) is left(string text, length)
Query Statementselect [column(s),] left(text,len)[from table]
Parameter description:
text: string, which can be directly quoted as a string or a field;
len: The length of the string to be intercepted, which is a positive integer. If it is 0 or a negative number, it returns a null value.
Description: The query enclosed in "[]" indicates optional.If you need to enter the from statement to specify the target table when querying with other fields, you also need to enter the from statement to specify the target table when text is a field. If you only use left to obtain a specified string to intercept, you can directly omit the from statement, but this usageGenerally only for debugging and not often used in practice.
(2)Usage: Intercept the characters of the specified length on the left.
(3) Example
Example 1: Intercept "hello" from "hello world".
select left('hello world',5)
There is a student table student, student ID, name, gender, native_place, nationality, date of birth born, age.as shown in the table below.
student
ID
name
sex
native_place
nation
born
age
1001
Zhang San
Male
Guangdong Province
Chinese
2000/3/16
22
1002
Chen Yi
Female
Guangdong
strong
1998/3/15
24
1003
Wang Wu
Male
Tibet
Hidden
2002/1/6
20
1004
Chen Hong
Female
Guangdong Province
Li
2001/1/17
21
1005
Li Si
Male
Northeast Province
Chinese
1999/3/16
23
1006
-
Female
Guangdong Province
Li
5/6/1998
24
1008
Chen Xiaoxiao
Female
Guangdong
strong
1997/5/9
25
Example 2: Remove the apostrophe from the place of origin, and intercept the first two characters.Obtain student ID, name, gender and age at the same time.
select ID,name,sex,left(native_place,2),agefrom student
2.MID function
(1) Grammar and usage: there are two forms
#①Two parametersmid(text,start) #Get characters from an ordinal of the specified string#②Three parametersmid(text,start,len) #Get characters of the specified length from a certain position in the specified string
Parameter description:
text: string, which can be directly quoted as a string or a field;
start: The string position to start intercepting, which can be a positive or negative integer.
len: The length of the string to be intercepted, which is a positive integer. If it is 0 or a negative number, it returns a null value.
Note: The MID function is synonymous with SUBSTRING(), and the usage is the same.
(2) Instance
Example: Intercept the string "world" from "hello world".
When start is a positive number, the position of the start character "w" is 7. Note that the empty character between "hello" and "world" is counted as one character.
#start is a positive number#two two parametersselect mid('hello world',7) ?orselect mid('hello world'from 7)#Three parametersselect mid('hello world',7,5)orselect mid('hello world'from 7 for 5)
3.RIGHT function
(1) Syntax: right(text,len) is right(string text, length)
Query Statementselect [column(s),] right(text,len)[from table]
Parameter description:
text: string, which can be directly quoted as a string or a field;
len: The length of the string to be intercepted, which is a positive integer. If it is 0 or a negative number, it returns a null value.
Description: The query enclosed in "[]" indicates optional.If you need to enter the from statement to specify the target table when querying with other fields, you also need to enter the from statement to specify the target table when text is a field. If you only use left to obtain a specified string to intercept, you can directly omit the from statement, but this usageGenerally only for debugging and not often used in practice.
(2)Usage: Intercept the characters of the specified length on the right.
(3) Example
Example 1: Intercept "world" from "hello world".
select right('hello world',5)
The usage of SQL's LEFT, MID and RIGHT functions is very similar to EXCEL's LEFT, MID and RIGHT functions. Please refer to:
边栏推荐
猜你喜欢
感情危机,朋友的网恋女友要和他闹分手,问我怎么办
A Method for Ensuring Data Consistency of Multi-Party Subsystems
“chmod 777-R 文件名”什么意思?
Redis Cluster - Sentinel Mode Principle (Sentinel)
【JWT】JWT 整合
SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
loadrunner-controller-目标场景Schedule配置
LeetCode二叉树系列——101.对称二叉树
Web系统常见安全漏洞介绍及解决方案-CSRF攻击
Build finished with errors/Executable Not Found
随机推荐
Progressive Web App(PWA)
梅科尔工作室--鸿蒙十四天开发培训笔记(八)
Android安全专题(三)JNI混淆
【23提前批】北森云计算-测开面经
C#之泛型、委托、事件及其使用
双链表的插入和删除
(C language) program environment and preprocessing
【职场杂谈】售前工程师岗位的理解杂谈
Web系统常见安全漏洞介绍及解决方案-sql注入
【LeetCode】118.杨辉三角
Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1
What is the encoding that starts with ?
NowCoderTOP17-22 Binary search/sort - continuous update ing
Redis的简单使用
Chapter VII
Echart饼图添加轮播效果
GZIPInputStream 类源码分析
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--开放虚拟网络(OVN)简介(课后练习)
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
【LeetCode】383.赎金信