MySQL FIELD Function

MySQL FIELD function is one of the String Functions, which returns the index position of the string (specified in the first argument) by looking in the remaining string expression(s). If all the arguments present in the MySQL string FILED function are integers, then they are compared as numbers. If they are strings, then they are compared to the string values.


The basic syntax of String FIELD in MySQL is as shown below:

SELECT FIELD (Str, Str1, Str2,....,StrN)
FROM Source

The above MySQL FIELD function returns the index position of the Str by checking Str1 to StrN based. For example, ELT(‘Hello’, ‘Hi’, ‘Hello’) return 2 as the output.


The FIELD function returns the index position of the specified string. The following field query shows various ways to use this FILED function.

-- MySQL String FIELD Function Example

-- Though the string at position 1 start with A, it is not equal to A
SELECT FIELD('A', 'ABC', 'DEF', 'G', 'H', 'I');

SELECT FIELD('MySQL', 'Learn', 'MySQL', 'Server', 'at', '');

SELECT FIELD('', 'Learn', 'MySQL', 'Server', 'at', '');

-- It returns 0 because there is no 'at' string 
SELECT FIELD('at', 'Learn', 'MySQL', 'Server');

-- It will return the index position of First Occurence
SELECT FIELD('MySQL', 'Learn', 'MySQL', 'Server', 'at', 'MySQL');
MySQL FIELD Function Example 1

Let me show you what happens if we specify integers and MySQL NULL values as the String Function arguments.

-- MySQL String FIELD Function Example
SELECT FIELD('2', '12', '13', '2', '15', '19');

SELECT FIELD(2, 12, 13, 2, 15, 19);

SELECT FIELD(2, 22, 222, 2222);

SELECT FIELD(NULL, 'Learn', NULL, 'MySQL', 'Tutorial');
MySQL FIELD Function Example 2