RPAD() Function in MySQL
RPAD() function in MySQL is used to pad or add a string to the right side of the original string.
Syntax :
RPAD(str, len, padstr)
Parameter : This function accepts three parameter as mentioned above and described below :
- str : The actual string which is to be padded. If the length of the original string is larger than the len parameter, this function removes the overfloating characters from string.
- len : This is the length of a final string after the right padding.
- padstr : String that to be added to the right side of the Original Str.
Returns : It returns a new string of length len after padding.
Example-1 : Applying RPAD() Function to a string to get a new padded string.
SELECT RPAD("geeksforgeeks", 20, "*") AS RightPaddedString;
Output :
| RightPaddedString |
|---|
| geeksforgeeks******* |
Example-2 : Applying RPAD() Function to a string when the original string is larger than the len parameter.
SELECT RPAD("geeksforgeeks", 10, "*") AS RightPaddedString;
Output :
| RightPaddedString |
|---|
| geeksforge |
Example-3 : Applying RPAD() Function to a string column in a table :
Table - Student_Details :
| Student_Id | Name | Marks |
|---|---|---|
| 1 | Tarun Saha | 430 |
| 2 | Nilima Mondal | 425 |
| 3 | Sanya Jain | 450 |
| 4 | Amit Sharma | 460 |
SELECT RPAD(Name, 15, "#") AS RightPadStudentName FROM Student_Details;
Output :
| RightPadStudentName |
|---|
| Tarun Saha##### |
| Nilima Mondal## |
| Sanya Jain##### |
| Amit Sharma#### |