MySQL FIELD() Function

The FIELD() function returns the index position of a value in a list of values. The search is case-insensitive. If the value is not found, the result is 0. If the value is NULL, the result is 0.

Tutorials dojo strip

MySQL FIELD Function Syntax

SQL

MySQL FIELD Function Example

This query returns the index position of ‘q’ in the list ‘s’, ‘q’, ‘l’.

SQL

Note: The FIELD() function is supported in MySQL but not in SQLite. This example does not work in the TechKubo playground.

MySQL FIELD Function Parameters

ParameterDescription
valueRequired. The value to search for in the list.
val1, val2, val3, …Required. The list of values to search in.

MySQL FIELD Function Table Column Example

This query attempts to return the index position of ‘F’ in the list ‘M’, ‘F’, ‘O’, using the gender column from the Patients table.

SQL

Note: The FIELD() function is supported in MySQL but not in SQLite. This example does not work in the TechKubo playground.

MySQL FIELD Function Notes

  • Returns 0 if the value is not found.
  • Returns 0 if the value is NULL.
  • The first position in the list is 1, not 0.
  • The search is case-insensitive.
  • Works in MySQL 4.0 and higher.
  • Not supported in SQLite. In SQLite, no direct equivalent exists.

Tutorials dojo strip