Appearance
REPEAT
Return a character string that is repeated n times.
REPEAT(string, number)
The first argument is any character string, or an expression that evaluates to a character string.
The second argument must be a literal number value (or an expression that evaluates to a literal number). You cannot specify a column name. A negative number is interpreted as zero. This function returns either a CHAR
or a VARCHAR
data type, depending on the input.
For example:
premdb=# select distinct repeat(city,3) from team where capacity>50000 order by 1;
repeat
--------------------------------
LondonLondonLondon
ManchesterManchesterManchester
NewcastleNewcastleNewcastle
(3 rows)
The following example shows the behavior of the function when character strings have trailing spaces. The season
table contains four columns:
premdb=# \d season
Table "public.season"
Column | Type | Modifiers
-------------+-----------------------+-----------
seasonid | smallint |
season_name | character(9) |
numteams | smallint |
winners | character varying(30) |
Two new rows are inserted:
premdb=# insert into season values(30, '2022-2023 ', 20, 'Unknown ');
INSERT 0 1
premdb=# insert into season values(31, '2023 ', 20, 'Unknown ');
INSERT 0 1
Trailing blanks are present in the values inserted into the season_name
(CHAR
) and winners
(VARCHAR
) columns. When the REPEAT
function is run on these columns, the trailing spaces are not preserved for season_name
, but they are preserved for winners
:
premdb=> select repeat(season_name,2), repeat(winners,2) from season where seasonid in(30,31);
repeat | repeat
--------------------+----------------------
20232023 | Unknown Unknown
2022-20232022-2023 | Unknown Unknown
(2 rows)
See also Trailing Blanks in Character Data.