Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

MySQL FIELD Function

by suresh

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.

MySQL FIELD Syntax

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.

MySQL FIELD Example

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', 'tutorialgateway.org');

SELECT FIELD('tutorialgateway.org', 'Learn', 'MySQL', 'Server', 'at', 'tutorialgateway.org');

-- 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

Placed Under: MySQL

  • How to Download MySQL
  • Install MySQL on Windows
  • MySQL Create Database
  • MySQL Delete Database
  • MySQL Create Table
  • MySQL Drop Table
  • MySQL SELECT Statement
  • MySQL ALIAS Column
  • MySQL Distinct
  • MySQL Insert Statement
  • MySQL Delete
  • MySQL Truncate Table
  • MySQL WHERE Clause
  • MySQL Order By
  • MySQL Group By
  • MySQL Having Clause
  • MySQL LIMIT
  • MySQL Arithmetic Operators
  • MySQL COALESCE Function
  • MySQL AND Operator
  • MySQL NOT Operator
  • MySQL OR Operator
  • MySQL XOR Operator
  • MySQL BETWEEN Operator
  • MySQL Not Between Operator
  • MySQL GREATEST Function
  • MYSQL LEAST Function
  • MySQL LIKE Operator
  • MySQL NOT LIKE Operator
  • MySQL IFNULL Operator
  • MySQL NULLIF Operator
  • MySQL INTERVAL Operator
  • MySQL IS Operator
  • MySQL IN Operator
  • MySQL NOT IN Operator
  • MySQL IS NOT NULL
  • MySQL IS NULL
  • MySQL Inner Join
  • MySQL Cross Join
  • MySQL Right Join
  • MySQL Left Join
  • MySQL Aggregate Functions
  • MySQL Date Functions
  • MySQL Date Function
  • MySQL String Functions
  • MySQL Numeric Functions

Copyright © 2021ยท All Rights Reserved.
About | Contact | Privacy Policy