Skip to content

Commit

Permalink
Add examples for FIELD() and FIND_IN_SET()
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Apr 2, 2024
1 parent 47abac9 commit eb87702
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit eb87702

Please sign in to comment.