The POSITION function is used to find the position of a substring in a main string, with positions counting from 1.
POSITION(<substr> IN <str>) POSITION(<substr>, <str> [, <pos>])
| Parameter | Description |
|---|---|
substr | The substring to search for. Type: VARCHAR |
str | The main string to search within. Type: VARCHAR |
pos | Optional parameter specifying the starting position (from 1). If specified, search starts from this position |
Returns INT type, representing the position of the first occurrence of the substring in the main string.
Search rules:
Special cases:
SELECT POSITION('bar' IN 'foobarbar'), POSITION('bar', 'foobarbar');
+----------------------------------+--------------------------------+ | POSITION('bar' IN 'foobarbar') | POSITION('bar', 'foobarbar') | +----------------------------------+--------------------------------+ | 4 | 4 | +----------------------------------+--------------------------------+
SELECT POSITION('bar', 'foobarbar', 5), POSITION('xbar', 'foobar');
+-----------------------------------+----------------------------------+ | POSITION('bar', 'foobarbar', 5) | POSITION('xbar', 'foobar') | +-----------------------------------+----------------------------------+ | 7 | 0 | +-----------------------------------+----------------------------------+
SELECT POSITION('test' IN NULL), POSITION(NULL, 'test');
+--------------------------+------------------------+ | POSITION('test' IN NULL) | POSITION(NULL, 'test') | +--------------------------+------------------------+ | NULL | NULL | +--------------------------+------------------------+
SELECT POSITION('' IN 'hello'), POSITION('world' IN '');
+------------------------+------------------------+ | POSITION('' IN 'hello') | POSITION('world' IN '') | +------------------------+------------------------+ | 1 | 0 | +------------------------+------------------------+
SELECT POSITION('World' IN 'Hello World'), POSITION('world' IN 'Hello World');
+----------------------------------+----------------------------------+ | POSITION('World' IN 'Hello World') | POSITION('world' IN 'Hello World') | +----------------------------------+----------------------------------+ | 7 | 0 | +----------------------------------+----------------------------------+
SELECT POSITION('a', 'banana', 1), POSITION('a', 'banana', 3);
+-----------------------------+-----------------------------+ | POSITION('a', 'banana', 1) | POSITION('a', 'banana', 3) | +-----------------------------+-----------------------------+ | 2 | 4 | +-----------------------------+-----------------------------+
SELECT POSITION('ḍḍumai' IN 'ṭṛì ḍḍumai hello'), POSITION('hello', 'ṭṛì ḍḍumai hello', 8);
+--------------------------------------+-------------------------------------------+ | POSITION('ḍḍumai' IN 'ṭṛì ḍḍumai hello') | POSITION('hello', 'ṭṛì ḍḍumai hello', 8) | +--------------------------------------+-------------------------------------------+ | 5 | 13 | +--------------------------------------+-------------------------------------------+
SELECT POSITION('@' IN 'user@domain.com'), POSITION('.', 'user@domain.com', 10);
+----------------------------------+--------------------------------------+ | POSITION('@' IN 'user@domain.com') | POSITION('.', 'user@domain.com', 10) | +----------------------------------+--------------------------------------+ | 5 | 12 | +----------------------------------+--------------------------------------+
SELECT POSITION('test', 'hello world', 20), POSITION('test', 'hello world', 0);
+--------------------------------------+-------------------------------------+ | POSITION('test', 'hello world', 20) | POSITION('test', 'hello world', 0) | +--------------------------------------+-------------------------------------+ | 0 | 0 | +--------------------------------------+-------------------------------------+
SELECT POSITION('123' IN '456123789'), POSITION('-', 'phone: 123-456-7890', 11);
+------------------------------+-------------------------------------------+ | POSITION('123' IN '456123789') | POSITION('-', 'phone: 123-456-7890', 11) | +------------------------------+-------------------------------------------+ | 4 | 11 | +------------------------------+-------------------------------------------+
The POSITION function is used to find the position of a substring within a string (counting from 1).
If the substring is not found, the function returns 0.
POSITION ( <substr> IN <str> ) POSITION ( <substr>, <str> [, <pos>] )
| Parameter | Description |
|---|---|
substr | The substring to search for |
str | The string to be searched |
pos | If this parameter is specified, the position of substr is searched from the string starting with the pos subscript |
The position of substr in str (counting from 1). If substr is not found, returns 0.
SELECT POSITION('bar' IN 'foobarbar'), POSITION('bar', 'foobarbar'), POSITION('bar', 'foobarbar', 5), POSITION('xbar', 'foobar');
+----------------------------------+--------------------------------+-----------------------------------+----------------------------------+ | position('bar' in 'foobarbar') | position('bar', 'foobarbar') | position('bar', 'foobarbar', 5) | position('xbar', 'foobar') | +----------------------------------+--------------------------------+-----------------------------------+----------------------------------+ | 4 | 4 | 7 | 0 | +----------------------------------+--------------------------------+-----------------------------------+----------------------------------+