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| |
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|
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|
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|
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!