MariaDB REPEAT() Function
In MariaDB, REPEAT()
is a built-in string function that repeats the specified string for the specified number of times and returns it.
MariaDB REPEAT()
Syntax
Here is the syntax of the MariaDB REPEAT()
function:
REPEAT(str, count)
Parameters
str
-
Required. A string that needs to be repeated.
count
-
Required. The number of repetitions required.
If you provide the wrong number of parameters, MariaDB will report an error: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
.
Return value
The MariaDB REPEAT(str, count)
function repeats the specified string for the specified number of times and returns it.
If count
is 0 or negative, the REPEAT()
function will return an empty string ''
.
If any of the arguments is NULL
, the REPEAT()
function will return NULL
.
MariaDB REPEAT()
Examples
Basic usage
The following statement shows how to use MariaDB REPEAT()
to generate a string including 3 'Go '
:
SELECT REPEAT('Go ', 3);
Output:
+------------------+
| REPEAT('Go ', 3) |
+------------------+
| Go Go Go |
+------------------+
Generate large strings
MariaDB REPEAT()
is useful for generating large strings for testing, such as:
SELECT CHAR_LENGTH(REPEAT('a', 3000));
Output:
+--------------------------------+
| CHAR_LENGTH(REPEAT('a', 3000)) |
+--------------------------------+
| 3000 |
+--------------------------------+
In this example, we used the REPEAT()
function to repeat a
3000 times, so the CHAR_LENGTH()
function returned 3000
.
Conclusion
In MariaDB, REPEAT()
is a built-in string function that repeats the specified string for the specified number of times and returns it.