MySQL REPEAT() Function
The REPEAT()
function in MySQL creates a new string by repeating an existing string a specified number of times. This is a very useful function for generating patterns, creating test data, or for string formatting tasks.
REPEAT(): Definition and Usage
REPEAT()
takes a string and an integer as input. The function returns a new string that's simply the original string repeated the specified number of times. If the number of repetitions is 0, it returns an empty string.
Syntax
Syntax
REPEAT(string, number)
Parameter Values
Parameter | Description |
---|---|
string |
The string to be repeated. This is required. |
number |
The number of times to repeat the string (must be a non-negative integer). This is required. |
Examples
Repeating a String Literal
This example repeats the string "SQL Tutorial" three times.
Syntax
SELECT REPEAT("SQL Tutorial", 3);
Output
SQL TutorialSQL TutorialSQL Tutorial
Repeating Strings from a Column
This example repeats the 'CustomerName' twice for each customer in the 'Customers' table (assuming a 'Customers' table exists with a 'CustomerName' column).
Syntax
SELECT REPEAT(CustomerName, 2)
FROM Customers;
Output
(Each CustomerName repeated twice will be displayed here.)
Repeating a String Zero Times
Repeating a string zero times returns an empty string.
Syntax
SELECT REPEAT("SQL Tutorial", 0);
Output