w3resource

MySQL REPEAT() function

REPEAT() function

MySQL REPEAT() repeats a string for a specified number of times. Both the string and the number of times that string to be repeated are supplied as arguments.

The function returns NULL either any of the arguments is NULL.

This function is useful in -

  • String repetition: It allows you to repeat a string a specified number of times.
  • Generating padding characters: REPEAT() is often used to generate padding characters. You can align text by repeating a specific character or string multiple times.

Syntax:

REPEAT(str, count)

Arguments:

Name Description
str A string which is to be repeated.
count An integer indicating how many times the string str is to be repeated.

Syntax Diagram:

MySQL REPEAT() Function - Syntax Diagram

MySQL Version: 8.0

Example: MySQL REPEAT() function

The following MySQL statement repeats the string ‘**-‘ 15 times.

Code:

SELECT REPEAT('**-',15); 

Output:

mysql> SELECT REPEAT('**-',15); 
+-----------------------------------------------+
| REPEAT('**-',15)                              |
+-----------------------------------------------+
| **-**-**-**-**-**-**-**-**-**-**-**-**-**-**- | 
+-----------------------------------------------+
1 row in set (0.03 sec)

Using REPEAT() with a column in a table

The following MySQL statement, the REPEAT() function is used with the aut_name column to repeat the auther names two times who are from the country USA.

Code:

SELECT aut_id, REPEAT(aut_name, 2) AS repeated_name 
FROM author
WHERE country='USA'

Sample table: author


Output:

aut_id|repeated_name             |
------+--------------------------+
AUT006|Thomas MertonThomas Merton|
AUT008|Nikolai DeweyNikolai Dewey|
AUT010|Joseph MiltonJoseph Milton|
AUT015|Butler AndreButler Andre  |

Video Presentation:

All String Functions (Slides presentation)

Previous: REGEXP
Next: REPLACE



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/string-functions/mysql-repeat-function.php