MySQL Position Function | Position of Substring in String

MySQL Position is an inbuilt function in MySQL. It returns the position of the first occurrence of a substring in a string. However, if the substring does not exist then it returns 0. Moreover, the function performs a case-insensitive search (uppercase and lowercase characters are the same). In this article, we will discuss the MySQL Position Function.

This function is an alias of MySQL Locate Function.

MySQL Position Function Diagram

Syntax

POSITION (substring IN string)

Parameters

The MySQL Position Function expects two parameters. The description of the parameters is as follows:

  • substring: The first parameter is the substring you want to search in the string.
  • string: The second parameter is the string in which you need to search.
  • start: The third and optional parameter is the start point. By default, the start point is 1.

Return Value

The function returns the position of first occurrence of a substring in a string. However, if the substring is not present, then it returns 0.

Example

mysql> SELECT POSITION("com" IN "concatly.com") as position;
+----------+
| position |
+----------+
|       10 |
+----------+

In the above example, we search for first occurrence of the substring “com” in “concatly.com”. The function returns the position of the first occurrence.

Conclusion

In conclusion, we discussed the MySQL Position. You can learn more about it on Official MySQL Documentation. Also, 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 *