FIELD() Function in MySQL
Last Updated :
05 Jan, 2021
FIELD() function :
This function in MySQL is used to return the index position of a specified value in a list of given values. For example, if the given list is (“3”, “1”, “2”) and the value is “1” for which index position is going to be search, then this function will return 2 as the index position.
Features :
- This function is used to find the index position of a given value in a list of specified values.
- This function accepts two types of parameters.
- First parameter is specified value to search for and second parameter is the specified list of values to search.
- This function does a case-insensitive search.
- If the given value is not present in the specified list of given values, this function returns 0.
Syntax :
FIELD(value, val1, val2, val3, ...)
Parameter :
This method accepts two parameters as given below :
-
value : Specified value to search for.
-
val1, val2, val3, … : Specified list of values to search.
Returns :
It return the index position of a specified value in a list of given values.
Example-1 :
Getting the index position 3 of the specified value 5 in the list of (“1”, “3”, “5”, “7”).
SELECT FIELD("5", "1", "3", "5", "7");
Output :
3
Example-2 :
Getting the index position 2 of the specified value 5 in the list of (“2”, “5”, “5”, “0”). Here in the list the value 5 is repeated two times but the function returns the index position of first time used value position only.
SELECT FIELD("5", "2", "5", "5", "0");
Output :
2
Example-3 :
Getting the index position 2 of the specified value “a” in the list of (“b”, “A”, “a”, “c”). Here alphabets “A” and “a” is used in the list but this function returns the index position of “A” for value “a” this happens because this function performs case-insensitive search.
SELECT FIELD("a", "b", "A", "a", "c");
Output :
2
Example-4 :
Getting the index position 3 of the specified value 4 in the numeric list of (0, 2, 4, 6, 8).
SELECT FIELD(4, 0, 2, 4, 6, 8);
Output :
3
Application :
This function is used to return the index position of a specified value in a list of given values.
Similar Reads
FIELD() function in MySQL
FIELD() : This function helps in returning the position of a value in the given value list. If the user passes string values as the argument of FIELD() function, then the search will be performed as string values. And, If the user passes numeric values as the argument of FIELD() function, then searc
2 min read
ELT() Function in MySQL
In this article, we are going to cover ELT function with examples. In ELT function, number field will state that how many strings will be there. ELT function in MySQL is used to returns the string which is at index number specified in the argument list. In this function there is number field and str
1 min read
DAY() Function in MySQL
DAY() function : This function in MySQL is used to return the day of the month for a specified date (a number from 1 to 31). This function equals the DAYOFMONTH() function. Syntax : DAY(date) Parameter : This method accepts a parameter which is illustrated below : date : Specified date to extract th
1 min read
DIV() Function in MySQL
DIV() function : This function in MySQL is used to return a quotient (integer) value when integer division is done. For example, when 7 is divided by 3, then 2 will be returned. Syntax : SELECT x DIV y; Parameter : This method accepts two parameters as given below as follows. x - Specified dividend
1 min read
CEILING() Function in MySQL
CEILING() function :This function in MySQL is used to return the smallest integer value that is greater than or equal to a specified number. For example, if the specified number is 4.6, this function will return the integer value of 5 that is greater than 4.6 or if a specified number is 5, this func
2 min read
ASCII() Function in MySQL
In this article, we are going to cover the ASCII function with examples and you will see the ASCII MYSQL query. And will also cover the ASCII code for the given character. Let's discuss one by one. ASCII function in MySQL is used to find the ASCII code of the leftmost character of a character expres
1 min read
CURTIME() function in MySQL
CURTIME() function in MySQL is used to check the current time. It returns the current time as a value in âhh:mm:ssâ or 'hhmmss' format, depending on whether the function is used in a string or numeric context. Syntax : CURTIME(fsp) Parameters : This method accepts only one parameter. fsp - It specif
2 min read
COT() Function in MySQL
COT() function : This function in MySQL is used to return the cotangent of a specified number. If the specified number is 0, an error or NULL will be returned. In a right triangle, the cotangent of an angle is the length of it's adjacent side divided by the length of the opposite side. Similarly, th
1 min read
DATEDIFF() Function in MySQL
DATEDIFF() function in MySQL is used to return the number of days between two specified date values. Syntax: DATEDIFF(date1, date2) Parameter: This function accepts two parameters as given below: date1: First specified datedate2: Second specified date Returns : It returns the number of days between
2 min read
AVG() Function in MySQL
AVG() function : This function in MySQL is used to return the average value of the specified expression. Features : This function is used to find the average value of the specified expression.This function comes under Numeric Functions.This function accepts only one parameter namely expression.This
2 min read