MySQL RPAD Function

MySQL RPAD is one of the String Functions, which is helpful to pad (or add) required string to the right side of the original sentence. 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 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 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 are going to use the customer details table data that we shown below

MySQL String RPAD Function Example

MySQL RPAD Example

The RPAD Function in MySQL 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');
MySQL RPAD Function Example 1

In this String method example, let me implement the string RPAD function on different columns in a table. The following MySQL statement adds Degree word at the end of the Education column, and attach 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;
MySQL RPAD Function Example 2