RPAD scalar function

The RPAD function returns a string that is padded on the right with blanks or a specified string.

Read syntax diagramSkip visual syntax diagramRPAD( string-expression, integer,pad)

The schema is SYSIBM.

The RPAD function returns a string composed of string-expression padded on the right, with pad or blanks. The RPAD function treats leading or trailing blanks in string-expression as significant. Padding will only occur if the actual length of string-expression is less than integer, and pad is not an empty string.

string-expression
An expression that specifies the source string. The expression must return a value that is a built-in string data type that is not a LOB.
integer
An integer constant that specifies the length of the result. The value must be zero or a positive integer that is less than or equal to n, where n is 32704 if string-expression is a character or binary string, or where n is 16352 if string-expression is a graphic string.
pad
An expression that specifies the string with which to pad. The expression must return a value that is a built-in string data type that is not a LOB. If pad is not specified, the pad character is determined as follows:
  • SBCS blank character if string-expression is a character string.
  • DBCS blank character if string-expression is a graphic string.
  • Hexadecimal zero (X'00'), if string-expression is a binary string.

The result of the function is a varying length string that has the same CCSID of string-expression. string-expression and pad must have compatible data types. If the string expressions have different CCSID sets, then pad is converted to the CCSID set of string-expression. If either string-expression or pad is FOR BIT DATA, no character conversion occurs. The actual length of the result is determined from integer.

The length attribute of the result depends on integer. If integer is greater than 0, the length attribute of the result is integer. If integer is 0, the length attribute of the result is 1.

The actual length of the result is determined from integer. If integer is 0, the actual length is 0, and the result is the empty string. If integer is less than the actual length of string-expression, the actual length is integer and the result is truncated.

The result can be null; if any argument is null, the result is the null value.

Example 1: Assume that NAME is a VARCHAR(15) column that contains the values 'Chris', 'Meg', and 'Jeff'. The following query will completely pad out a value on the right with periods.
  SELECT RPAD(NAME,15,'.' ) AS NAME 
    FROM T1;
The results are similar to the following output:
  NAME
  ---------------

  Chris..........
  Meg............
  Jeff...........
Example 2: Similar to Example 1, the following query will completely pad out a value on the right with pad (note that in some cases there is a partial instance of the padding specification):
  SELECT RPAD(NAME,15,'123' ) AS NAME 
    FROM T1;
The results are similar to the following output:
  NAME
  ---------------

  Chris1231231231
  Meg123123123123
  Jeff12312312312
Example 3: Similarly, the following query will only pad each value to a length of 5:
  SELECT RPAD(NAME,5,'.') AS NAME 
    FROM T1;
The results are similar to the following output:
  NAME
  ---------------

  Chris
  Meg..
  Jeff.
Example 4: Assume that NAME is a CHAR(15) column that contains the values 'Chris', 'Meg', and 'Jeff'. Note that the result of RTRIM in the following example is a varying length string with the blanks removed:
  SELECT RPAD(RTRIM(NAME),15,'.') AS NAME 
    FROM T1;
The results are similar to the following output:
  NAME
  ---------------
  Chris..........
  Meg............
  Jeff...........