MySQL RPAD() function
In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples.
Introduction of MySQL RPAD() function
RPAD() function in MySQL is used to pad or add a string to the right side of the original string.
Syntax of the MySQL RPAD() function
The syntax of the MySQL RPAD() function is given as follows:
RPAD(str, len, padstr)
Parameters or arguments used in MySQL RPAD() function:
There are three parameters accepted by the RPAD() function in MySQL, which are given as follows:
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 a string.
Len: This is the length of a final string after the right padding.
padstr: String that is to be added to the right side of the Original Str.
Returns:
It will return a new string of length len after padding.
Application used for RPAD() function:
The RPAD() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL RPAD() function:
Now we will look into some MySQL RPAD() function examples and will explore how we can use the RPAD function in MySQL.
Example-1:
In this example, we will Apply the RPAD() function to a string to get a new padded string in MySQL.
SELECT RPAD("JAVATPOINT", 20, "*") AS RightPaddedString;
Output:
RightPaddedString
JAVATPOINT**********
1 row in set (0.00 sec)
Example 2:
In this example, we will Apply the RPAD() function to a string when the original string is larger than the len parameter.
SELECT RPAD("JAVATPOINT", 6, "*") AS RightPaddedString;
Output:
RightPaddedString
JAVATP
1 row in set (0.00 sec)
Example-3:
When we require a new padded string of column data, we can take the help of the RPAD function.
Pupil_Id | Name | Marks |
1 | Tarun Saha | 430 |
2 | Nilima Mondal | 425 |
3 | Sanya Jain | 450 |
4 | Amit Sharma | 460 |
Table – Pupil_Details
SELECT RPAD(Name, 15, "#") AS RightPadPupilName
FROM Pupil_Details;
Output:
RightPadPupilName
Tarun Saha#####
Nilima Mondal##
Sanya Jain#####
Amit Sharma####
Application of MySQL RPAD() function:
This function is used to pad or add a string to the right side of the original string.
Summary:
In the above context, we have learned how we can use the RPAD() function in MySQL used to pad or add a string to the right side of the original string.