rpad()

The rpad() function pads the right side of a string by repeating a padding string and returns a result of the specified length.

Syntax

rpad(STR, LENGTH[, PAD])

Parameters

STR
The original string field or value. Non-string values are converted to a string before processing.
LENGTH
The target length of the result string. Must be an integer between 1 and 10240 inclusive. If specified as a constant that is out of range, a parsing error is raised. If the value is out of range at runtime, null is returned.
PAD
(Optional) The padding string to fill on the right. If omitted, a space () is used. If an empty string ("") is specified, it is treated as a space.

Description

The rpad() function pads the right side of STR by repeating PAD until the total length reaches LENGTH, and returns the result.

  • If STR is null, the function returns null.
  • If STR is longer than LENGTH, the function truncates STR to LENGTH characters and returns the result.
  • If LENGTH is null or not an integer, the function returns null.
  • If PAD is null, the function returns null.
  • If PAD consists of multiple characters, it is repeated and then trimmed to exactly LENGTH characters.

Error codes

Error codeDescription
90890A constant less than 1 or greater than 10240 was specified for LENGTH.

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Right-pad HTTP methods with spaces to 10 characters

    table limit=5 WEB_APACHE_SAMPLE | eval result = rpad(method, 10) | fields method, result
    | # result: "GET       ", "POST      ", etc.
    
  2. Pad with a specific character

    table limit=5 WEB_APACHE_SAMPLE | eval result = rpad(method, 8, "-") | fields method, result
    | # result: "GET-----", "POST----", etc.
    
  3. Use a multi-character padding string

    json "{'val': 'hello'}" | eval result = rpad(val, 10, "pad")
    | # result: "hellopadpa"
    
  4. Original string is longer than the target length

    json "{'val': 'hello'}" | eval result = rpad(val, 3, "x")
    | # result: "hel"
    
  5. NULL input

    json "{'val': null}" | eval result = rpad(val, 8, "*")
    | # result: null
    

Compatibility

The rpad() function is available since before Sonar 4.0.