The REPEAT function is used to repeat a specified string a specified number of times to generate a new string. This function is commonly used for generating padding characters, creating separators, or generating test data.
REPEAT(<str>, <count>)
| Parameter | Description |
|---|---|
<str> | The source string to repeat. Type: VARCHAR |
<count> | The number of times to repeat, must be a non-negative integer. Type: INT |
Returns VARCHAR type, representing the string repeated the specified number of times.
Repeat rules:
Special cases:
SELECT REPEAT('a', 3);
+----------------+ | REPEAT('a', 3) | +----------------+ | aaa | +----------------+
SELECT REPEAT('hello', 2);
+--------------------+ | REPEAT('hello', 2) | +--------------------+ | hellohello | +--------------------+
SELECT REPEAT('test', 0);
+-------------------+ | REPEAT('test', 0) | +-------------------+ | | +-------------------+
SELECT REPEAT('a', -1);
+-----------------+ | REPEAT('a', -1) | +-----------------+ | | +-----------------+
SELECT REPEAT(NULL, 3), REPEAT('a', NULL);
+------------------+-------------------+ | REPEAT(NULL, 3) | REPEAT('a', NULL) | +------------------+-------------------+ | NULL | NULL | +------------------+-------------------+
SELECT REPEAT('', 5);
+----------------+ | REPEAT('', 5) | +----------------+ | | +----------------+
SELECT REPEAT('-', 10), REPEAT('*', 5);
+------------------+-----------------+ | REPEAT('-', 10) | REPEAT('*', 5) | +------------------+-----------------+ | ---------- | ***** | +------------------+-----------------+
SELECT REPEAT('ṭṛì', 3), REPEAT('ḍḍu', 2);
+-------------------+-------------------+ | REPEAT('ṭṛì', 3) | REPEAT('ḍḍu', 2) | +-------------------+-------------------+ | ṭṛìṭṛìṭṛì | ḍḍuḍḍu | +-------------------+-------------------+
SELECT REPEAT('123', 3), REPEAT('@#', 4);
+-------------------+------------------+ | REPEAT('123', 3) | REPEAT('@#', 4) | +-------------------+------------------+ | 123123123 | @#@#@#@# | +-------------------+------------------+
SELECT REPEAT('ṭṛìṭṛì', 3);
+--------------------------------------------------+ | REPEAT('ṭṛìṭṛì', 3) | +--------------------------------------------------+ | ṭṛìṭṛìṭṛìṭṛìṭṛìṭṛì | +--------------------------------------------------+
The REPEAT function is used to repeat a string a specified number of times.
REPEAT( <str>, <count> )
| Parameter | Description |
|---|---|
<str> | The string to be repeated. |
<count> | The number of times to repeat. It must be a non-negative integer. If it is less than 1, an empty string will be returned. |
Returns the string repeated the specified number of times. Special cases:
SELECT repeat("a", 3);
+----------------+ | repeat('a', 3) | +----------------+ | aaa | +----------------+
SELECT repeat("a", -1);
+-----------------+ | repeat('a', -1) | +-----------------+ | | +-----------------+