MySQL RPAD is one of the String Function, which is used to pad (or add) required string to the right side of the original string. For example, to pad common information after the description for each row then use this RPAD. In this article we will show you, How to use string RPAD in MySQL with example.
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 right padding). If the final string (after right padding) is greater than this length value then extra characters will be trimmed. Remember, You can use Length function to find the string length.
- Pad_String: String that you want to add to right side of the Original_Str
In order 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 is used to pad the string expression to the right side of the original string. The following query will show multiple ways to use this 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. Following statement will add Degree string at the end of Education column, and attach dollar symbol after each and every 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;
Thank You for Visiting Our Blog