PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL REPEAT() Function



The PostgreSQL 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.

SELECT REPEAT('A', 3);
Result: 'AAA'

SELECT REPEAT('1', 5);
Result: '11111'

SELECT REPEAT('ABC', 2);
Result: 'ABCABC'

SELECT REPEAT('ABC', 0);
Result: ''

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(CAST(EmpID AS VARCHAR), 3) AS NewEmpID 
FROM Employee;

This will produce the result as shown below:

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

❮ PostgreSQL Functions