MySQL REPEAT Function | Repeat Strings

MySQL REPEAT is an inbuilt function in MySQL. It repeats a string for a specified number of times. Both the string and the number are the arguments to the function. In this article, we will discuss the MySQL REPEAT Function. Also, we will discuss a few examples of using it.

Syntax

REPEAT(stringnumber)

Parameters

The MySQL REPEAT expects two mandatory parameters. The description of the parameters is as follows:

  • string: The first parameter to the function is the string you want to repeat.
  • number: The second parameter to the function is the number of times you want to repeat the string.

Return Value

The function returns a string after repeating the specified string the given number of times.

MySQL Repeat Function

Examples

Let’s discuss a few examples of using the function.

Example 1: Simple SELECT Query

mysql> SELECT REPEAT('Concatly', 3);
+--------------------------+
| REPEAT('Concatly', 3)    |
+--------------------------+
| ConcatlyConcatlyConcatly |
+--------------------------+

In the above example, we repeat a string ‘Concatly’ 3 times.

Example 2: Repeating a Column

Similarly, you can also repeat a column of the table.

mysql> SELECT REPEAT(user_name, 2) FROM user_description;
+----------------------+
| REPEAT(user_name, 2) |
+----------------------+
| chandlerchandler     |
| monicamonica         |
| rossross             |
+----------------------+

Example 3: Passing 0 as the Number

However, if you pass 0 as the number to the function, the function returns an empty string.

mysql> SELECT REPEAT('Concatly', 0);
+-----------------------+
| REPEAT('Concatly', 0) |
+-----------------------+
|                       |
+-----------------------+

Conclusion

In this article, we discussed the MySQL REPEAT Function. You can read more about it on MySQL Official Documentation. Additionally, you can read more about String Functions in MySQL on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *