当前位置:网站首页>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
边栏推荐
- General Lei's personal blog to see
- Distributed lock development
- go : delete database data using grom
- 专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录
- LSF提交作业命令--bsub
- typescript8-类型注解
- typescript1 - what is typescript
- typescript6-简化运行ts的步骤
- 解决datagrip连接sqlserver报错:[08S01] 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。
- 孙洪鹤讲教材:原点+众筹+产品,逆向营销实战操作方案
猜你喜欢
随机推荐
interface
C语言自定义类型详解
MagicDraw二次开发过程
golang: Gorm configures Mysql multiple data sources
typescript5-编译和安装ts代码
【小程序专栏】总结uniapp开发小程序的开发规范
Selected as one of the "Top Ten Hard Core Technologies", explaining the technical points of Trusted Confidential Computing (TECC) in detail
2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
【Kotlin 中的类和继承】
Fix datagrip connection sqlserver error: [08S01] The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption.
Input method for programmers
【COCI 2020/2021 Round #2 D】Magneti (DP)
AutoSAR EcuM系列02- Fixed EcuM的状态管理
General Lei's personal blog to see
便携小风扇PD取电芯片
docker部署redis一主二从三哨兵模式
typescript8-类型注解
SQL窗口函数
svn中文路径 权限设定
mysql8的my.conf配置文件参考指引









