MySQL RPAD is one of the String Function, which is helpful to pad (or add) required string to the right side of the original string. For example, we use this string RPAD in MySQL to pad standard information after the description for each row.
MySQL RPAD Syntax
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 string data that is available in a column.
- Length: This is the length of a final string after the right padding. If the final string (after right padding) is higher than this length value, the extra characters trimmed. Remember, You can use the Length function to find the string length.
- Pad_String: String that you want to add to the right side of the Original_Str
To demonstrate this RPAD string function, we are going to use the customer details table data that we shown below
MySQL RPAD Example 1
The RPAD Function in MySQL pad the string expression to the right side of the original string. The following RPAD query displays multiple ways to use this string RPAD function.
SELECT RPAD('Hello ', 11, 'World'); -- It will trim the final padded string 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');
MySQL RPAD Function Example 2
In this example, let me implement the string RPAD function on different columns in a table. The following statement adds Degree string at the end of the Education column, and attach dollar symbol after each yearly Income row.
-- MySQL String RPAD Example 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;