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,
nullis 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
STRisnull, the function returnsnull. - If
STRis longer thanLENGTH, the function truncatesSTRtoLENGTHcharacters and returns the result. - If
LENGTHisnullor not an integer, the function returnsnull. - If
PADisnull, the function returnsnull. - If
PADconsists of multiple characters, it is repeated and then trimmed to exactlyLENGTHcharacters.
Error codes
| Error code | Description |
|---|---|
| 90890 | A 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.
-
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. -
Pad with a specific character
table limit=5 WEB_APACHE_SAMPLE | eval result = rpad(method, 8, "-") | fields method, result | # result: "GET-----", "POST----", etc. -
Use a multi-character padding string
json "{'val': 'hello'}" | eval result = rpad(val, 10, "pad") | # result: "hellopadpa" -
Original string is longer than the target length
json "{'val': 'hello'}" | eval result = rpad(val, 3, "x") | # result: "hel" -
NULL input
json "{'val': null}" | eval result = rpad(val, 8, "*") | # result: null
Compatibility
The rpad() function is available since before Sonar 4.0.