FIELD()

Overview

Returns the position of the first argument's string in the subsequent string arguments.

USAGE

FIELD(find, value[, value] ...)

Argument Name

Description

find

The value that is being searched for

value

One or more values to search through

DETAILS

FIELD() is a string function that returns the index of the find argument from a list of arguments.

The return value is the 1-based index position of the first match in the list or 0 if it is not found.

The arguments must be strings, numbers, or doubles.

If the items being compared do not match in type, an inexact comparison is performed. For instance, the string 5-fold will match the integer 5 regardless of which one is the find term and which one is the list term, but if both arguments are strings they do not match.

Searching for NULL always returns 0.

SYNONYMS

SCHEMA

PARAMETERS

SKYSQL

PRIVILEGES

EXAMPLES

SELECT FIELD('sad', 'good', 'bad', 'neutral');
+----------------------------------------+
| FIELD('sad', 'good', 'bad', 'neutral') |
+----------------------------------------+
|                                      0 |
+----------------------------------------+
SELECT FIELD('bad', 'good', 'bad', 'neutral');
+----------------------------------------+
| FIELD('bad', 'good', 'bad', 'neutral') |
+----------------------------------------+
|                                      2 |
+----------------------------------------+
-- Disable strict mode or the select might throw an error
SET sql_mode = '';
SELECT FIELD('5-fold', 'good', 5, 'bad', '5-fold');
+---------------------------------------------+
| FIELD('5-fold', 'good', 5, 'bad', '5-fold') |
+---------------------------------------------+
|                                           2 |
+---------------------------------------------+

Precision

Precision is a factor when comparing function arguments. As illustrated by the following example, a double value is accurate up to 15 decimal places.

SELECT FIELD(1, 1.000000000000001, 1);
+--------------------------------+
| FIELD(1, 1.000000000000001, 1) |
+--------------------------------+
|                              2 |
+--------------------------------+
SELECT FIELD(1, 1.0000000000000001, 1);
+---------------------------------+
| FIELD(1, 1.0000000000000001, 1) |
+---------------------------------+
|                               1 |
+---------------------------------+

ERROR HANDLING

FEATURE INTERACTION

RESPONSES

DIAGNOSIS

ISO 9075:2016

CHANGE HISTORY

Release Series

History

23.09

  • Present starting in MariaDB Xpand 23.09.1.

6.1

  • Present starting in MariaDB Xpand 6.1.0.

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.1

  • Present starting in MariaDB Xpand 6.1.0.

EXTERNAL REFERENCES