All Products
Search
Document Center

MaxCompute:RPAD

Last Updated:Feb 05, 2024

If you want to format strings in a data table to ensure string consistency and alignment, you can use the RPAD function of MaxCompute. This function right pads str1 with str2 to the specified length. This function is an additional function of MaxCompute V2.0. This topic describes the syntax of the RPAD function and provides an example on how to use this function.

Syntax

string rpad(string <str1>, int <length>, string <str2>)

Parameters

  • str1: required. A value of the STRING type. This parameter specifies the string that you want to right pad.

  • length: required. A value of the INT type. This parameter specifies the number of characters that are used for padding.

  • str2: required. This parameter specifies the string that you use to right pad another string.

Return value

A value of the STRING type is returned. The return value varies based on the following rules:

  • If the value of length is less than the number of characters in str1, this function truncates str1 from the left to obtain a string with the number of characters specified by length.

  • If length is set to 0, an empty string is returned.

  • If no input parameters are available or an input parameter is set to null, null or an error is returned.

  • If the value of length is greater than the number of characters in str1 and less than the sum of the number of characters in str1 and str2, str1 is right paded with str2 from left to right to the specified length.

Examples

  • Example 1: Right pad the string abcdefgh with the string 12 to obtain a string with 10 characters in length. Sample statements:

    -- The return value is abcdefgh12. 
    SELECT rpad('abcdefgh', 10, '12');
  • Example 2: Right pad the string abcdefgh with the string 12 to obtain a string with 5 characters in length. Sample statements:

    -- The return value is abcde. 
    SELECT rpad('abcdefgh', 5, '12');
  • Example 3: The value of length is 0. Sample statements:

    -- The return value is an empty string. 
    SELECT rpad('abcdefgh' ,0, '12'); 
  • Example 4: An input parameter is set to null. Sample statements:

    -- The return value is null. 
    SELECT rpad(null ,0, '12');
  • Example 5: Right pad the string abcdefgh with the string 12 from left to right to obtain a string that contains nine characters. Sample statements:

    -- The return value is abcdefgh1.
    SELECT rpad('abcdefgh',9,'12');

Related functions

RPAD is a string function. For more information about functions related to string searches and conversion, see String functions.