Skip to content

Commit

Permalink
Add MAKE_SET() and LTRIM() examples (#16954) (#17313)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Apr 23, 2024
1 parent 679b7e2 commit 7f5748f
Showing 1 changed file with 126 additions and 2 deletions.
128 changes: 126 additions & 2 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1238,11 +1238,135 @@ SELECT LPAD('TiDB',-2,'>');

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

Remove leading spaces.
The `LTRIM()` function removes leading spaces from a given string.

If the argument is `NULL`, this function returns `NULL`.

> **Note:**
>
> This function only removes the space character (U+0020) and does not remove other space-like characters such as tab (U+0009) or non-breaking space (U+00A0).
Examples:

In the following example, the `LTRIM()` function removes the leading spaces from `' hello'` and returns `hello`.

```sql
SELECT LTRIM(' hello');
```

```
+--------------------+
| LTRIM(' hello') |
+--------------------+
| hello |
+--------------------+
1 row in set (0.00 sec)
```

In the following example, [`CONCAT()`](#concat) is used to enclose the result of `LTRIM(' hello')` with `«` and `»`. This formatting makes it a bit easier to see that all leading spaces are removed.

```sql
SELECT CONCAT('«',LTRIM(' hello'),'»');
```

```
+------------------------------------+
| CONCAT('«',LTRIM(' hello'),'»') |
+------------------------------------+
| «hello» |
+------------------------------------+
1 row in set (0.00 sec)
```

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

Return a set of comma-separated strings that have the corresponding bit in bits set.
The `MAKE_SET()` function returns a set of comma-separated strings based on whether a corresponding bit in the `bits` argument is set to `1`.

Syntax:

```sql
MAKE_SET(bits, str1, str2, ...)
```

- `bits`: controls which subsequent string arguments to include in the result set. If `bits` is set to `NULL`, the function returns `NULL`.
- `str1, str2, ...`: a list of strings. Each string corresponds to a bit in the `bits` argument from right to left. `str1` corresponds to the first bit from the right, `str2` corresponds to the second bit from the right, and so on. If the corresponding bit is `1`, the string is included in the result; otherwise, it is not included.

Examples:

In the following example, because all bits are set to `0` in the `bits` argument, the function does not include any subsequent strings in the result and returns an empty string.

```sql
SELECT MAKE_SET(b'000','foo','bar','baz');
```

```
+------------------------------------+
| MAKE_SET(b'000','foo','bar','baz') |
+------------------------------------+
| |
+------------------------------------+
1 row in set (0.00 sec)
```

In the following example, because only the first bit from the right is `1`, the function only returns the first string `foo`.

```sql
SELECT MAKE_SET(b'001','foo','bar','baz');
```

```
+------------------------------------+
| MAKE_SET(b'001','foo','bar','baz') |
+------------------------------------+
| foo |
+------------------------------------+
1 row in set (0.00 sec)
```

In the following example, because only the second bit from the right is `1`, the function only returns the second string `bar`.

```sql
SELECT MAKE_SET(b'010','foo','bar','baz');
```

```
+------------------------------------+
| MAKE_SET(b'010','foo','bar','baz') |
+------------------------------------+
| bar |
+------------------------------------+
1 row in set (0.00 sec)
```

In the following example, because only the third bit from the right is `1`, the function only returns the third string `baz`.

```sql
SELECT MAKE_SET(b'100','foo','bar','baz');
```

```
+------------------------------------+
| MAKE_SET(b'100','foo','bar','baz') |
+------------------------------------+
| baz |
+------------------------------------+
1 row in set (0.00 sec)
```

In the following example, because all bits are `1`, the function returns all three strings in a comma-separated result set.

```sql
SELECT MAKE_SET(b'111','foo','bar','baz');
```

```
+------------------------------------+
| MAKE_SET(b'111','foo','bar','baz') |
+------------------------------------+
| foo,bar,baz |
+------------------------------------+
1 row in set (0.0002 sec)
```

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

Expand Down

0 comments on commit 7f5748f

Please sign in to comment.