MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

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:

EmpIDNameCity
1JohnLondon
2MarryNew York
3JoParis
4KimAmsterdam
5RameshNew Delhi
6HuangBeijing

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:

EmpIDNameCityNewEmpID
1JohnLondon111
2MarryNew York222
3JoParis333
4KimAmsterdam444
5RameshNew Delhi555
6HuangBeijing666

❮ MySQL Functions