当前位置:网站首页>SQL substring_index() usage - MySQL string interception
SQL substring_index() usage - MySQL string interception
2022-07-30 08:39:00 【embelfe_segge】
Table of Contents
1. The syntax of the substring_index function and its usage
(1) Syntax: substring_index(string,sep,num)
(2) is used to intercept the target string.
(1) From aField intercepts the target string.
1.The syntax of the substring_index function and its usage
(1) Syntax: substring_index(string,sep,num)
That is, substring_index (string, separator, serial number)
Parameter description
string: The string used to intercept the target string.Can be fields, expressions, etc.
sep: Separator, a character that exists and is used to separate strings, such as ",", ".", etc.
num: serial number, which is a non-zero integer.If it is an integer, it counts from left to right, and if it is a negative number, it counts from right to left.For example, "www.mysql.com" intercepts the character 'www', the separator is ".", and the sequence number from left to right is 1, that is, substring_index("www.mysql.com",'.',1);To start getting "com", the serial number is -1, that is, substring_index("www.mysql.com",'.',-1)
(2) is used to intercept the target string.
2.Instance
(1) Intercept the target string from a field.
Example: There is a student information table student, and the detailed address address stores the address information separated by commas such as province, city, county, such as "XX province, XX city, XX district, ..., XXX number".For some reason, there is no information about the province where the student is located. At the same time, the student's name, gender, and age are obtained.
select name,sex,age,substring_index(address,',',1) as provincefrom student
(2) Combine with the cast function to intercept a string and convert it to the target format.
Example: There is an existing order information data. Since the format of the date information stored after the date of 2022-03-04 is wrong, some prefixes are added before the date and separated by spaces, such as "13d 2022-02-01".Obtain the specific date information after 2022-03-04 in the table, and obtain the list offer_id and product name at the same time.
select cast(substring_index(ctime,' ',1) as date) as dt,offer_id,nameFROM dataWHERE substring_index(ctime,' ',1)>= '2022-03-04'
For the usage of cast function in SQL, please refer toSQL's CAST() - Converting Data Types_'s Blog-CSDN Blog
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
函数(1)
风靡全球25年的重磅IP,新作沦为脚本乐园
立创EDA——PCB的走线(五)
2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
typescript6-简化运行ts的步骤
你好,我的新名字叫 “铜锁 / Tongsuo”
The difference between typescript3-ts and js
IDEA search plug-in has no results and the solution has been spinning in circles
What convenience does the RFID fixed asset inventory system bring to enterprises?
万字详解:C语言三子棋进阶 + N子棋递归动态判断输赢(另附课设大作业参考)
随机推荐
golang grpc protoc 环境配置
mysql8的my.conf配置文件参考指引
stack containing min function (js)
C语言力扣第46题之全排列。回溯法
MagicDraw二次开发过程
Mybitatis相关配置文件
Link with Bracket Sequence II(杭电多校赛)
入选“十大硬核科技”,详解可信密态计算(TECC)技术点
Gorm 更新零值问题
Charles通过Rewrite越过OPTIONS请求拦截
【小程序专栏】总结uniapp开发小程序的开发规范
Lenovo Notebook How to Change Windows 10 Boot Logo Icon
mysql设置会话超时时间
sizeof
实现定时器
谷粒商城--环境部署(2022/7/28最新)
IDEA搜索插件无结果一直转圈圈的解决办法
RT-Thread-GD32的SPI在切换同一总线不同挂载设备时会出现切换后乱发送数据的问题
【微信小程序】页面事件
如何实时计算日累计逐单资金流