MySQL POSITION Function

The MySQL Position function is a synonym of Locate Function. This Position String Function is useful to find the index position of the first occurrence of a substring in a string expression.

Let me show you how to write String Position in MySQL to find the substring index position with an example. The basic syntax of string Position in MySQL is as shown below:

POSITION(Substring IN String)

MySQL Position Function Example 1

The String Position Function finds the index position of a substring. The following query shows multiple ways to use this String Position function.

SELECT POSITION('a' IN 'tutorialgateway');

SELECT POSITION('g' IN 'tutorialgateway');

SELECT POSITION('o' IN 'Hello World');
MySQL POSITION Function 1

It is another example of this MySQL position function.

SELECT POSITION('abc' IN 'This is abc working in abc Company');

SELECT POSITION('in' IN 'This is abc working in abc Company');

SELECT POSITION('is' IN 'This is abc working in abc Company');
MySQL POSITION Function 2

MySQL Position String Example 2

In this String Function example, we implement this string Position on different columns present in the employe table. The following MySQL statement finds the first occurrence of @, com, and mail words from the Email column. Next, it returns the position.

SELECT FirstName,
LastName,
      DepartmentName,
      Email,
      POSITION('@' IN Email),
      POSITION('com' IN Email),
      POSITION('mail' IN Email) AS Mail
FROM `MySQL Tutorial`.employe;
MySQL POSITION Function 3

Please refer to MySQL Locate Function article.