Postgresql Rpad Function

PostgreSQL: rpad Function

In this PostgreSQL post explains how to use the PostgreSQL rpad function with syntax and examples.

Description

The PostgreSQL rpad function returns a string that is right-padded with a specified string to a certain length.

Syntax

The syntax for the rpad function in PostgreSQL is:

rpad( string, length, pad_string )

Parameters or Arguments

string

The string to right-pad.

length

The length of the result after string has been right-padded.

pad_string

The specified string to right-pad to string.

Note

  • If string is longer than length, the rpad function will remove characters from the left-hand side of string to shorten it to length characters.
  • See also the lpad function.

Applies To

The rpad 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 rpad function examples and explore how to use the rpad 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 rpad function returns in PostgreSQL. If you ran these commands yourself, you would not see the single quotes in the result):

postgres=# SELECT rpad('AODBA.com', 11, 'A');
        rpad
----------------------

 'AODBA.comAA'
(1 row)

postgres=# SELECT rpad('AODBA.com', 12, 'A');
        rpad
-----------------------

 'AODBA.comAAA'
(1 row)

postgres=# SELECT rpad('abc', 6, ' ');
   rpad
----------

 'abc   '
(1 row)

postgres=# SELECT rpad('abc', 9, 'XYZ');
    rpad
-------------

 'abcXYZXYZ'
(1 row)

postgres=# SELECT rpad('abc', 10, 'XYZ');
    rpad
--------------

 'abcXYZXYZX'
(1 row)