Home > Database > Mysql Tutorial > body text

How to use the INSTR() function in MySQL

王林
Release: 2023-05-28 18:10:06
forward
3367 people have browsed it

INSTR()

INSTR(str,substr)The function is used to return the index position of the first occurrence of the substring substr in the string str. It is returned when the substring is not found. 0. For example:

select INSTR('MySQL字符串函数', '字符串') AS index1,
       INSTR('MySQL字符串函数', '日期') AS index2,
       INSTR('MySQL字符串函数', '') AS index3,
       INSTR('MySQL字符串函数', null) AS index4;
index1|index2|index3|index4|
------+------+------+------+
     6|     0|     1|      |
Copy after login

In addition, the LOCATE(substr,str) function can also be used to return the index position of the first occurrence of substring substr in the string str, and INSTR(str, substr) function is that the order of parameters is reversed.

LOCATE(substr,str,pos)The function returns the index position of the first occurrence of substring substr in the string str starting from position pos, for example:

SELECT LOCATE('S','MySQL Server', 5) AS ind;
ind|
---+
  7|
Copy after login

FIELD(str,str1,str2,str3,…) The function returns the position where the string str appears in the subsequent string list. If it is not found, it returns 0. For example:

SELECT FIELD('李四', '张三', '李四', '王五') AS ind;
ind|
---+
  2|
Copy after login

FIND_IN_SET(str,strlist) The function returns the position where the string str appears in the list string strlist. strlist consists of N substrings separated by commas. For example:

SELECT FIND_IN_SET('李四', '张三,李四,王五') AS ind;
ind|
---+
  2|
Copy after login

The above is the detailed content of How to use the INSTR() function in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!