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. Please refer to the MySQL LOCATE Function article.

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

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

SELECT POSITION('o' IN 'Hello World');
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');
Position 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.

TIP: Also, read about the MySQL SUBSTRING and MySQL INSTR methods from the list of available MySQL String functions.

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