RPAD

RPAD is a SQL function used to right-align characters by including specific characters to the right of the string until it reaches a certain length. The filled characters are added to the original string's end, providing the right padding to it.

RPAD(str, len, padstr)

  • str: This parameter refers to the string that needs to be right-padded with the specified characters.
  • len: This parameter signifies the total length of the resulting string after padding. If the ‘len’ specified is smaller than the length of the original string, it would be cut to the specified ‘len’.
  • padstr: This is the string that will be padded to the right of the original string. If the string of ‘padstr’ is not long enough to reach the total length ‘len’, it will be repeated until the required length is reached.

Example

SELECT RPAD('SQL', 6, '1');

Output

'SQL111'

Explanation

In the above example, the RPAD function is used to pad the right side of the string ‘SQL’ to a length of 6 characters with the character ‘1’. The result is ‘SQL111’.

RPAD(string1 VARCHAR2 CHARACTER SET ANY_CS, pad_length NUMBER, pad_string VARCHAR2 CHARACTER SET string1%CHARSET) RETURN VARCHAR2 CHARACTER SET string1%CHARSET;

  • string1 varchar2 character set any_cs: This is the input string that is to be padded on the right. The VARCHAR2 datatype represents a variable-length character string where the size is defined in either bytes (BYTE keyword) or characters (CHAR keyword).
  • pad_length number: This parameter specifies the length to which the original string is to be padded. If the pad_length is less than the length of the string1, then this function will trim off extra characters from the end of string1 to make its length equivalent to pad_length.
  • pad_string varchar2 character set string1%charset: This is the string that the function uses to pad string1. If the length of the pad_string is less than the required number of padding characters, then the function replicates the pad string up to the required length. If pad_string is not specified, the function uses a single space as the default pad string.
  • return varchar2 character set string1%charset: This is the output of the function - the original string1 padded on the right up to the pad_length with the pad_string. It is of datatype VARCHAR2 and it has the same character set as string1.

Example

SELECT RPAD('SQL', 5, 'X') AS Padded_String
FROM dual;

Output

Padded_String
-------------
SQLXX

Explanation

The RPAD function pads the right side of a string with a specified set of characters. In this example, ‘SQL’ is the string to be padded, 5 is the total length that the string should be after padding, and ‘X’ is the padding character. So, ‘SQL’ is padded on the right with ‘X’ to create a string of total length 5, yielding the result ‘SQLXX’.

RPAD(character, length, fill)

  • character: This refers to the string that is to be padded.
  • length: This parameter determines the length of the result string. If the value is less than the length of the character string, removal of excess bytes or characters from the end of the string occurs.
  • fill: This is the padding character used. If this parameter is omitted, the function will use spaces by default. If the fill string is not long enough to satisfy the length requirement, the fill string is repeated as many times as necessary.

Example

SELECT RPAD('Hello', 10, '!');

Output

'Hello!!!!!'

Explanation

The RPAD function pads the right side of a string with a specific set of characters. In this case, an exclamation point ’!’ is added to the right side of ‘Hello’ until it reaches a total length of 10.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.