POSITION() FUNCTION in MySQL

POSITION() FUNCTION
The MySQL POSITION function is used to get the location of a substring in a string. The various versions of MySQL support the POSITION function, namely, MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0 and MySQL 3.23.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
POSITION( substring IN string )
POSITION( substring IN string )
POSITION( substring IN string )

Parameters:
substring: It is used to specify the substring to search for.
string: It is used to specify the string to search.

Example 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT POSITION ( ‘LIN 'HELLO WORLD' );
mysql> SELECT POSITION ( ‘L’ IN 'HELLO WORLD' );
mysql> SELECT POSITION ( ‘L’ IN 'HELLO WORLD' );

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
3
3
3

Explanation:
The position of the first appearance of the substring is returned as the result.

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT POSITION ( ‘l’ IN 'HELLO WORLD' );
mysql> SELECT POSITION ( ‘l’ IN 'HELLO WORLD' );
mysql> SELECT POSITION ( ‘l’ IN 'HELLO WORLD' );

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
3
3
3

Explanation:
The position of the first appearance of the substring is returned as the result.