REPEAT

Description

The REPEAT function is used to repeat a string str times times and return the generated string.

Parameters

  • str (string type): The string to be repeated.
  • times (int type): The number of times the string str needs to be repeated.

Return Type

Returns a string that is the result of repeating str times times.

Example Usage

  1. Repeating a single character:
SELECT REPEAT('a', 3);

Results:

aaa
  1. Repeat a word: {#repeat-a-word}
SELECT REPEAT('hello', 2);

Results:

hellohello
  1. Use with other functions, such as concatenating strings:
SELECT CONCAT('The word " ', REPEAT('repeat', 2), ' " appears multiple times.');

Results:

The word " repeatrepeat " appears multiple times.

Notes

  • Ensure the times parameter is a positive integer, otherwise the function will return an empty string.
  • When times is 0, the function returns an empty string.
  • If the str or times parameters are not of the expected type, the function may return an error or an empty string.

By using the REPEAT function, you can easily create strings with repeating patterns, which is very useful for generating test data or padding strings.