MySQL POSITION Function

The MySQL Position function is a synonym of Locate method. This 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

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 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');
MySQL POSITION Function 2

MySQL Position String 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 `MySQL Tutorial`.employe;
MySQL POSITION Function 3