How REPEAT() Works in MariaDB

In MariaDB, REPEAT() is a built-in string function that returns a string repeated a certain number of times.

You pass the string, as well as the number of times you want it repeated when you call the function.

Syntax

The syntax goes like this:

REPEAT(str,count)

Where str is the string to repeat, and count is the number of times to repeat it.

Example

Here’s a basic example:

SELECT REPEAT('Pow!', 3);

Result:

+-------------------+
| REPEAT('Pow!', 3) |
+-------------------+
| Pow!Pow!Pow!      |
+-------------------+

Concatenation with Other Strings

In this example we pass REPEAT() as an argument to the CONCAT() function so that some dots are prepended and appended to the string:

SELECT CONCAT(REPEAT('.', 12), 'Dog', REPEAT('.', 12));

Result:

+-------------------------------------------------+
| CONCAT(REPEAT('.', 12), 'Dog', REPEAT('.', 12)) |
+-------------------------------------------------+
| ............Dog............                     |
+-------------------------------------------------+

Low Count

If the count is less than 1, the REPEAT() function returns an empty string:

SELECT 
    REPEAT('Pow!', 0),
    REPEAT('Pow!', -1);

Result:

+-------------------+--------------------+
| REPEAT('Pow!', 0) | REPEAT('Pow!', -1) |
+-------------------+--------------------+
|                   |                    |
+-------------------+--------------------+

Empty String

Here’s what happens when an empty string is passed:

SELECT REPEAT('', 100);

Result:

+-----------------+
| REPEAT('', 100) |
+-----------------+
|                 |
+-----------------+

Space Character

An empty string is not the same as the space character though.

Here’s what happens when we change the empty string to a space:

SELECT REPEAT(' ', 100);

Result:

+------------------------------------------------------------------------------------------------------+
| REPEAT(' ', 100)                                                                                     |
+------------------------------------------------------------------------------------------------------+
|                                                                                                      |
+------------------------------------------------------------------------------------------------------+

Null Argument

Providing null results in null:

SELECT 
    REPEAT(null, 5),
    REPEAT('Dog', null);

Result:

+-----------------+---------------------+
| REPEAT(null, 5) | REPEAT('Dog', null) |
+-----------------+---------------------+
| NULL            | NULL                |
+-----------------+---------------------+

Missing Argument

Calling REPEAT() with the wrong number of arguments, or without passing any arguments results in an error:

SELECT REPEAT();

Result:

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