PostgreSQL: rpad Function
This PostgreSQL tutorial 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('techonthenet.com', 18, 'A'); rpad ---------------------- 'techonthenet.comAA' (1 row) postgres=# SELECT rpad('techonthenet.com', 19, 'A'); rpad ----------------------- 'techonthenet.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)
Advertisements