MySQL RPAD is one of the String Functions, which is helpful to pad (or add) the required string to the right side of the original sentence. For example, we use this string RPAD to pad standard information after the description for each row.
The basic syntax of the string RPAD Function in MySQL is as shown below:
SELECT RPAD (Original_Str, length, Pad_String) FROM [Source]
- Original_Str: This is the original data that is available in a column.
- Length: This is the length of a final one after the right padding. If the final sentence (after right padding) is higher than this length value, the extra characters are trimmed. Remember, You can use the Length function to find the string length.
- Pad_String: substring that you want to add to the right side of the Original_Str
To demonstrate this RPAD function, we will use the customer details table data we have shown below.
MySQL RPAD Function Example
The RPAD Function pad the string expression to the right side of the original sentence. The following query displays multiple ways to use this one.
SELECT RPAD('Hello ', 11, 'World'); -- It will trim the final padded to length 7 SELECT RPAD('Hello', 7, 'World'); -- Let me use NULL value as input SELECT RPAD('Hello', 7, NULL); SELECT RPAD(NULL, 9, 'Hello');
In this String method example, let me implement the string RPAD function on different columns in a table. The following MySQL statement adds the Degree word at the end of the Education column and attaches the dollar symbol after each yearly Income row.
USE company; SELECT First_Name, Last_Name, Education, RPAD(Education, 14, ' Degree') AS Edu_Details, Profession, Yearly_Income, RPAD(Yearly_Income, 6, ') AS Dollar_Income, Sales FROM customerdetails;