MySQL INSTR | Case Insensitive Position of String in String

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.

Syntax

INSTR(stringsubstring)

Parameters

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.

Return Value

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.

MySQL INSTR Function

Examples

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

Conclusion

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.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *