PostgreSQL 18 reference · String Functions
PostgreSQL REPEAT() Function
Repeat a string a given number of times. A count of 0 or less gives an empty string.
PostgreSQL 18
Syntax
repeat(string, number)
Parameters
| Parameter | Description |
|---|---|
string |
The input text. |
number |
How many times. |
Return value
Text.
Examples
SELECT repeat('ab', 3) AS a, repeat('=', 10) AS rule;
Output
a | rule --------+------------ ababab | ========== (1 row)
Run this on your computer — see “Get Started” for setup.
SELECT name, repeat('*', (stock / 20)::int) AS stock_bar
FROM products
WHERE category_id = 2;
Output
name | stock_bar ---------------------+----------- Mechanical Keyboard | * Wireless Mouse | *** 4K Monitor | USB-C Hub | * (4 rows)
Run this on your computer — see “Get Started” for setup.
Related
LPAD()· Pad a string on the left to a given length with spaces or another string. Longer strings are cut to that length.RPAD()· Pad a string on the right to a given length with spaces or another string. Longer strings are cut to that length.- SQL String Functions · tutorial