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 REPEAT() example (#16960) #17107

Merged
Merged
Changes from all 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
56 changes: 56 additions & 0 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,62 @@ Return the substring that matches the regular expression (Partly compatible with

Repeat a string the specified number of times.

Examples:

The following example generates a sequence of numbers from 1 to 20 using a [recursive common table expression (CTE)](/develop/dev-guide-use-common-table-expression.md#recursive-cte). For each number in the sequence, the character `x` is repeated the number of times equal to the number itself.

```sql
WITH RECURSIVE nr(n) AS (
SELECT 1 AS n
UNION ALL
SELECT n+1 FROM nr WHERE n<20
)
SELECT n, REPEAT('x',n) FROM nr;
```

```
+------+----------------------+
| n | REPEAT('x',n) |
+------+----------------------+
| 1 | x |
| 2 | xx |
| 3 | xxx |
| 4 | xxxx |
| 5 | xxxxx |
| 6 | xxxxxx |
| 7 | xxxxxxx |
| 8 | xxxxxxxx |
| 9 | xxxxxxxxx |
| 10 | xxxxxxxxxx |
| 11 | xxxxxxxxxxx |
| 12 | xxxxxxxxxxxx |
| 13 | xxxxxxxxxxxxx |
| 14 | xxxxxxxxxxxxxx |
| 15 | xxxxxxxxxxxxxxx |
| 16 | xxxxxxxxxxxxxxxx |
| 17 | xxxxxxxxxxxxxxxxx |
| 18 | xxxxxxxxxxxxxxxxxx |
| 19 | xxxxxxxxxxxxxxxxxxx |
| 20 | xxxxxxxxxxxxxxxxxxxx |
+------+----------------------+
20 rows in set (0.01 sec)
```

The following example demonstrates that `REPEAT()` can operate on strings consisting of multiple characters.

```sql
SELECT REPEAT('ha',3);
```

```
+----------------+
| REPEAT('ha',3) |
+----------------+
| hahaha |
+----------------+
1 row in set (0.00 sec)
```

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

Replace occurrences of a specified string.
Expand Down
Loading