MySQL POSITION Function

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

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

POSITION(Substring IN String)

MySQL Position Function Example

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

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

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

SELECT POSITION('o' IN 'Hello World');
MySQL Position function Example 1

It is another example of this.

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');
Example 2

position Example 2

In this example method, 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. Please refer to the Locate Function article.

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