lpad()
The lpad() function pads the left side of a string by repeating a padding string and returns a result of the specified length.
Syntax
Parameters
STR- The original string to apply padding to.
LENGTH- The total length of the returned string. Must be between
1and10240inclusive. If specified as a constant that is out of range, error code90890is raised. PAD_STR- The padding string to fill on the left. If not specified, a space character (
) is used. If an empty string ("") is specified, it is treated as a space character.
Description
The lpad() function pads the left side of STR by repeating PAD_STR until the total length reaches LENGTH, and returns the result. If STR is longer than or equal to LENGTH, the function returns LENGTH characters from the left side of STR.
If STR is null, the function returns null. If STR is not a string, it is converted using toString() before processing.
If LENGTH is specified as a variable and its value is null, not an integer, less than 1, or greater than 10240, the function returns null.
If PAD_STR is specified as a variable and its value is null, the function returns null.
If LENGTH is specified as a constant that is less than 1 or greater than 10240, error code 90890 is raised and query parsing fails.
Error codes
90890- Raised when
LENGTHis specified as a constant that is less than1or greater than10240.
Usage examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Left-pad HTTP status codes with spaces to 5 characters
table limit=5 WEB_APACHE_SAMPLE | eval result = lpad(string(status), 5) | fields status, result | # result: " 200", " 404", etc. -
Specify a padding character
table limit=5 WEB_APACHE_SAMPLE | eval result = lpad(string(status), 5, "0") | fields status, result | # result: "00200", "00404", etc. -
Specify a multi-character padding string
json "{'val': 'string'}" | eval result = lpad(val, 10, "pad") | # result: "padpstring" -
Original string is longer than LENGTH (truncated from the left)
json "{'val': 'string'}" | eval result = lpad(val, 3, "pad") | # result: "str" -
NULL input
json "{'val': null}" | eval result = lpad(val, 10) | # result: null
Compatibility
The lpad() function is available since before Sonar 4.0.