From eb87702953626b55d53e3c8e7e7bdafcca782d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Tue, 2 Apr 2024 10:30:14 +0200 Subject: [PATCH] Add examples for FIELD() and FIND_IN_SET() --- functions-and-operators/string-functions.md | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index d6c9f6bf1ad7b..7bd0b3d6c2a48 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -416,10 +416,42 @@ Return a string such that for every bit set in the value bits, you get an on str Return the index (position)of the first argument in the subsequent arguments. +```sql +SELECT FIELD('needle', 'A', 'needle', 'in', 'a', 'haystack'); +``` + +``` ++-------------------------------------------------------+ +| FIELD('needle', 'A', 'needle', 'in', 'a', 'haystack') | ++-------------------------------------------------------+ +| 2 | ++-------------------------------------------------------+ +1 row in set (0.00 sec) +``` + +Here the first argument of `FIELD()` is `needle` and this matches the second one of the following arguments, so the function returns 2. + ### [`FIND_IN_SET()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set) Return the index position of the first argument within the second argument. +```sql +SELECT FIND_IN_SET('Go', 'COBOL,BASIC,Rust,Go,Java,Fortran'); +``` + +``` ++-------------------------------------------------------+ +| FIND_IN_SET('Go', 'COBOL,BASIC,Rust,Go,Java,Fortran') | ++-------------------------------------------------------+ +| 4 | ++-------------------------------------------------------+ +1 row in set (0.00 sec) +``` + +In the example above, `Go` is the 4th element of the set, so the function returns 4. + +This function is often used with the [`SET`](/data-type-string.md#set-type) data type. + ### [`FORMAT()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_format) The `FORMAT(X,D[,locale])` function is used to format the number `X` to a format similar to `"#,###,###. ##"`, rounded to `D` decimal places, and return the result as a string.