Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ELT and EXPORT_SET examples #16934

Merged
merged 17 commits into from
Apr 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,63 @@ Output:

### [`ELT()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_elt)

Return string at index number.
Returns the element at the index number.
qiancai marked this conversation as resolved.
Show resolved Hide resolved

```sql
SELECT ELT(3, 'This', 'is', 'TiDB');
```

```
dveeden marked this conversation as resolved.
Show resolved Hide resolved
+------------------------------+
| ELT(3, 'This', 'is', 'TiDB') |
+------------------------------+
| TiDB |
+------------------------------+
1 row in set (0.00 sec)
```

This example returns the third element, which is "TiDB".
dveeden marked this conversation as resolved.
Show resolved Hide resolved

### [`EXPORT_SET()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_export-set)

Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string.

The full signature of this function is:

```
EXPORT_SET(bits, on, off, [separator, number_of_bits])
dveeden marked this conversation as resolved.
Show resolved Hide resolved
dveeden marked this conversation as resolved.
Show resolved Hide resolved
```
qiancai marked this conversation as resolved.
Show resolved Hide resolved

dveeden marked this conversation as resolved.
Show resolved Hide resolved
```sql
SELECT EXPORT_SET(b'00001111', 'x', '_', '', 8);
```

```
qiancai marked this conversation as resolved.
Show resolved Hide resolved
+------------------------------------------+
| EXPORT_SET(b'00001111', 'x', '_', '', 8) |
+------------------------------------------+
| xxxx____ |
+------------------------------------------+
1 row in set (0.00 sec)
```

In the example above `bits` is set to `00001111` and this causes the function to return `____` for the bits that are set to 0 and `xxxx` for the bits that are set to 1. Here `x` means "on" and "_" means "off".
dveeden marked this conversation as resolved.
Show resolved Hide resolved

```sql
SELECT EXPORT_SET(b'01010101', 'x', '_', '', 8);
```

```
dveeden marked this conversation as resolved.
Show resolved Hide resolved
+------------------------------------------+
| EXPORT_SET(b'01010101', 'x', '_', '', 8) |
+------------------------------------------+
| x_x_x_x_ |
+------------------------------------------+
1 row in set (0.00 sec)
```

In the example above you can see the off (`_`) / on (`x`) pattern from right to left as set by the `bits.
dveeden marked this conversation as resolved.
Show resolved Hide resolved

### [`FIELD()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_field)

Return the index (position)of the first argument in the subsequent arguments.
Expand Down
Loading