rpad()

A system function for padding the right side of a string

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

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)

Categories

String manipulation, System function

See also

lpad()