Contents
rpad()
rpad()
is a system function for expanding a string by expanding the right side with one or more occurrences of a character or string (by default a space).
rpad()
was added in PostgreSQL 6.1.
Usage
rpad ( string text, length integer [, fill text ] ) → text
string will be expanded to length characters by padding the right side with a space, or if specified with fill, a character or string. If more than one character is specified in fill, the string will be repeated in the padding, and truncated if necessary.
If string is already longer than length, then string will be truncated (on the right-hand side) to length.
Change history
- PostgreSQL 6.1
- added (commit 83978e1e)
Examples
Basic usage of rpad()
:
postgres=# SELECT rpad('foo', 6); rpad -------- foo (1 row)
Using a non-default padding character:
postgres=# SELECT rpad('foo', 6, '!'); rpad -------- foo!!! (1 row)
Using a padding string:
postgres=# SELECT rpad('foo', 8, '!?'); rpad ---------- foo!?!?! (1 row)
Attempting to pad a string which is longer than the specified padding length:
postgres=# SELECT rpad('foobar', 3); rpad ------ foo (1 row)
Attempting to pad a string to a negative length:
postgres=# SELECT rpad('foobar', -3); rpad ------ (1 row)
References
- PostgreSQL documentation: Other String Functions