The MySQL Instr is one of the String Functions, which returns the position of the first occurrence of a specified substring from a given sentence.
This article shows how to write this with an example, and the basic syntax of the Instr string function is as shown below.
SELECT INSRT (St, Substring) FROM Source
- Str: A valid string or expression in which you want to look.
- Substring: text that you are looking for.
The Instr function in MySQL returns the index position of the first occurrence of a Substring in an Str. If then Substring is not found, it returns 0 as output.
MySQL Instr String Example
The following query shows multiple ways to use this method using different string values.
Within this example, the first statement, we used it to find the index position of the first occurrence of a
In the last MySQL statement, we are looking for a substring that does not exist.
SELECT INSTR('TutorialGateway', 'a') AS Instr1; SELECT INSTR('MySQL Tutorial at Tutorial Gateway', 'Tutorial') AS Instr2; SELECT INSTR('We are abc working at abc company', 'abc') AS Instr3; SELECT INSTR('We are abc working at abc company', 'MySQL') AS Instr4;
The MySQL String Instr function also allows you to write the index position from column values too. To demonstrate this function, We are going to use the below-shown data.
In this example, we are going to use this function to find the index position of the High word in the Education column.
SELECT EmpID, FirstName, LastName, Education, INSTR(Education, 'High') AS InsrtFunction, Occupation, YearlyIncome, Sales FROM employee;