PostgreSQL – RPAD Function

The rpad function in PostgreSQL database is used to right-pad a string with a specified character or a set of characters until the resulting string reaches a specified length. This function is often used to format strings to a fixed width.

Here’s the syntax for the rpad function:

rpad(input_string, length, pad_string)
  • input_string is the original string that you want to pad.
  • length is the total length of the resulting string after padding. If the input string is longer than this length, it will not be truncated.
  • pad_string is the character or string that will be used for padding on the right side of the input string.

The rpad function takes the input_string and adds pad_string characters to its right side until the resulting string is length characters long.

Here’s an example of how to use the rpad function:

SELECT rpad('42', 5, '0');

In this example, the rpad function is used to right-pad the string '42' with zeros ('0') until it reaches a length of 5 characters. The result of this query will be '42000'.

You can use rpad to format strings to a fixed length by adding padding characters to the right side.

Similar Posts