当前位置:网站首页>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:
边栏推荐
猜你喜欢
感情危机,朋友的网恋女友要和他闹分手,问我怎么办
如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
loadrunner-controller-手动场景Schedule配置
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
Mybaits Frequently Asked Questions Explained
Principle of Redis Sentinel
Mybaits 常用问题详解
LeetCode二叉树系列——101.对称二叉树
Redis缓存面临的缓存雪崩问题
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
随机推荐
loadrunner脚本--添加事务
Dart Log工具类
一种用于保证多方子系统数据一致性的方法
Echart饼图添加轮播效果
win10镜像下载
【LeetCode】36.有效的数独
P5231 [JSOI2012]玄武密码(SAM 经典运用)
【GORM】存取数组/自定义类型数据
SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
loadrunner-controller-view script与load generator
Windows安装mysql详细步骤(通俗易懂,简单上手)
loadrunner-Controller负载测试-各模块功能记录01测试场景设计
Mybaits Frequently Asked Questions Explained
SQL力扣刷题五
逆置问题--重点
【23提前批】北森云计算-测开面经
【职场杂谈】售前工程师岗位的理解杂谈
darknet 源码阅读笔记-01-activation_kernels.cu
出色的移动端用户验证
[ verb phrase ] collection