当前位置:网站首页>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 student2.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 stringParameter 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:
边栏推荐
猜你喜欢
随机推荐
第五章
Make your own dataset in FCN and train it
双链表的插入和删除
前序、后序及层次遍历实现二叉树的序列化与反序列化
逆置问题--重点
loadrunner-controller-view script与load generator
一种用于保证多方子系统数据一致性的方法
【LeetCode】383.赎金信
实现线程池
Windows系统Mysql8版本的安装教程
KVM虚拟化作业
Android安全专题(三)JNI混淆
如何判断自己是否适合IT行业?方法很简单
迪拜的超市---线段树双重懒标记+二分
nodeJs--url模块
SQL学习笔记——REGEXP运算符
Come n times - 07. Rebuild the binary tree
SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
【LeetCode】118.杨辉三角
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备








