totn PostgreSQL Functions

PostgreSQL: repeat Function

This PostgreSQL tutorial explains how to use the PostgreSQL repeat function with syntax and examples.

Description

The PostgreSQL repeat function repeats a string a specified number of times.

Syntax

The syntax for the repeat function in PostgreSQL is:

repeat( string, number )

Parameters or Arguments

string
The string to repeat.
number
The number of times to repeat the string.

Note

  • If number is less than 1, the repeat function will return an empty string.

Applies To

The repeat function can be used in the following versions of PostgreSQL:

  • PostgreSQL 9.4, PostgreSQL 9.3, PostgreSQL 9.2, PostgreSQL 9.1, PostgreSQL 9.0, PostgreSQL 8.4

Example

Let's look at some PostgreSQL repeat function examples and explore how to use the repeat function in PostgreSQL.

For example:

(Please note that for each of the example results below, we have included single quotes around the result to demonstrate what the repeat function returns in PostgreSQL. If you ran these commands yourself, you would not see the single quotes in the result):

postgres=# SELECT repeat('techonthenet.com', 2);
              repeat
------------------------------------
 'techonthenet.comtechonthenet.com'
(1 row)

postgres=# SELECT repeat('A', 2);
 repeat
--------
 'AA'
(1 row)

postgres=# SELECT repeat('a', 5);
 repeat
---------
 'aaaaa'
(1 row)

postgres=# SELECT repeat(' ', 6);
 repeat
--------
 '      '
(1 row)