RPAD
The RPAD function in SQL pads the right side of a string with a specific set of characters.
Syntax
RPAD(string, length, pad_string)
Parameters
string: The original string to pad.length: The length of the resulting string after padding. If this is less than the length of the original string, the result is truncated.pad_string(optional): The string to use for padding. If not supplied, spaces are used.
Return Value
The function returns a new string that is the same as the original string, but with additional padding on the right side to achieve the specified length.
Errors
- If the
stringargument is not a string, aFunctionRequiresStringValueerror will be returned. - If the
lengthargument is not a positive integer, aFunctionRequiresUSizeValueerror will be returned.
Examples
Consider a table Item created and filled with the following data:
CREATE TABLE Item (
name TEXT
);
INSERT INTO Item VALUES ('hello');
You can use the RPAD function to pad the name values to a length of 10 with the character 'b':
SELECT RPAD(name, 10, 'b') AS padded_name FROM Item;
This will return:
hellobbbbb
If the length argument is less than the length of the string, the original string will be truncated:
SELECT RPAD(name, 3, 'b') AS padded_name FROM Item;
This will return:
hel