MySQL REPEAT() Function
The MySQL REPEAT() function repeats a string a specified number of times.
Syntax
REPEAT(string, number)
Parameters
string |
Required. Specify the string to repeat. |
number |
Required. Specify the number of times to repeat the string. |
Return Value
Returns the repeated version of the specified string.
Example 1:
The example below shows the usage of REPEAT() function.
mysql> SELECT REPEAT('A', 3); Result: 'AAA' mysql> SELECT REPEAT(1, 5); Result: 11111 mysql> SELECT REPEAT('ABC', 2); Result: 'ABCABC' mysql> SELECT REPEAT('ABC', 0); Result: '' mysql> SELECT REPEAT(' ', 5); Result: ' '
Example 2:
Consider a database table called Employee with the following records:
EmpID | Name | City |
---|---|---|
1 | John | London |
2 | Marry | New York |
3 | Jo | Paris |
4 | Kim | Amsterdam |
5 | Ramesh | New Delhi |
6 | Huang | Beijing |
In the query below, the REPEAT() function is used to get the repeated value of EmpID column value of Employee table.
SELECT *, REPEAT(EmpID, 3) AS NewEmpID FROM Employee;
This will produce the result as shown below:
EmpID | Name | City | NewEmpID |
---|---|---|---|
1 | John | London | 111 |
2 | Marry | New York | 222 |
3 | Jo | Paris | 333 |
4 | Kim | Amsterdam | 444 |
5 | Ramesh | New Delhi | 555 |
6 | Huang | Beijing | 666 |
❮ MySQL Functions