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 12 commits
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
78 changes: 76 additions & 2 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,85 @@ 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)
```

The preceding example returns the third element, which is `'TiDB'`.

### [`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.
`EXPORT_SET()` returns a string that consists of a specified number (`number_of_bits`) of `on`/`off` values for `bits` starting from the right-most (lowest) bit as the first value, optionally separated by `separator`.
dveeden marked this conversation as resolved.
Show resolved Hide resolved

Syntax:

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

- `bits`: an integer representing the bit value.
- `on`: the string to be returned if the corresponding bit is `1`.
- `off`: the string to be returned if the corresponding bit is `0`.
- `separator` (optional): the separator character in the result string.
- `number_of_bits` (optional): the number of bits to be processed. If it is not set, `64` (the max size of bits) is used by default, which means that `bits` is treated as an unsigned 64-bit integer.

Examples:

In the following example, `number_of_bits` is set to `5`, resulting in 5 values, separated by `|`. Because only 3 bits are given, the other bits are considered unset. Therefore, setting `number_of_bits` to either `101` or `00101` results in the same output.

```sql
SELECT EXPORT_SET(b'101',"ON",'off','|',5);
```

```
qiancai marked this conversation as resolved.
Show resolved Hide resolved
+-------------------------------------+
| EXPORT_SET(b'101',"ON",'off','|',5) |
+-------------------------------------+
| ON|off|ON|off|off |
+-------------------------------------+
1 row in set (0.00 sec)
```

dveeden marked this conversation as resolved.
Show resolved Hide resolved
In the following example, `bits` is set to `00001111`, `on` is set to `x`, and `off` is set to `_`. This causes the function to return `____` for the `0` bits and `xxxx` for the `1` bits. Therefore, when processing with the bits in `00001111` from right to left, the function returns `xxxx____`.

```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 following example, `bits` is set to `00001111`, `on` is set to `x`, and `off` is set to `_`. This causes the function to return `x` for each `1` bit and `_` for each `0` bit. Therefore, when processing with the bits in `01010101` from right to left, the function returns `x_x_x_x_`.

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

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

Expand Down
Loading