MySQL INSTR Function is an inbuilt function in MySQL. It finds the first position of occurrence of a substring in another string. Moreover, it performs the search in a case-insensitive manner. In this article, we will discuss the MySQL INSTR Function. Also, we will discuss a few examples of using it.
The MySQL INSTR Function expects two parameters. The description of the parameters is as follows:
- string: The first parameter to the function is a string in which to perform the search.
- substring: The string to search for in the original string.
The function returns the integer position of the first occurrence of a substring in the string. However, it returns 0 if the substring is not present in the string.
Example 1: Simple Search
For instance, consider a simple substring.
mysql> SELECT INSTR("concatly.com", "com") AS position; +----------+ | position | +----------+ | 10 | +----------+
In the above example, the function returns the position of “com” in the original string.
Example 2: Case Insensitive Search
Similarly, we can observe that the function searches in a case-insensitive manner.
mysql> SELECT INSTR("concatly.com", "COM") AS position; +----------+ | position | +----------+ | 10 | +----------+
Example 3: Substring Not Found
If the substring does not exist in the original string, the function returns 0.
mysql> SELECT INSTR("concatly.com", "xyz") AS position; +----------+ | position | +----------+ | 0 | +----------+
In conclusion, we discussed the MySQL INSTR Function in this article. You can read more about it on the Official MySQL Documentation. Additionally, you can learn about more MySQL String Functions on Concatly.
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.