MySQL RPAD Function | Pad a String with Another String

MySQL RPAD is an inbuilt function in MySQL. It pads a string with specified characters on the right up to a given length. In this article, we will discuss the MySQL RPAD Function. Also, we will discuss a few examples of using it.

You can also read about MySQL LPAD Function.

Syntax

RPAD (string, length, paddingString)

Parameters

The MySQL RPAD Function expects three parameters. Moreover, all the parameters are mandatory.

  • string: The first parameter specifies the original string.
  • length: The second parameter specifies the length of the final string.
    • If the length is smaller than the original length of the string, then the function removes the extra characters.
    • If the length is greater than the original length of the string, the function pads the padding string from right.
  • paddingString: The string to left pad the original string.

Return Value

The MySQL LPAD returns a string after padding it with the specified string up to the given length from the right.

MySQL RPAD Function

Examples

Let’s discuss a few examples of using the function.

Example 1: Length is Greater than Original Length

mysql> SELECT RPAD("Concatly", 15, "ABC") as paddedString;
+-----------------+
| paddedString    |
+-----------------+
| ConcatlyABCABCA |
+-----------------+

In the above example, the function pads “ABC” from right of the original string to make a total of 15 characters.

Example 2: Length is Smaller than Original Length

Alternatively, if the length is smaller than the original length of the string, the function removes the extra characters.

mysql> SELECT RPAD("Concatly", 3, "ABC") as paddedString;
+--------------+
| paddedString |
+--------------+
| Con          |
+--------------+

Conclusion

In conclusion, we discussed the MySQL RPAD Function. You can read more about it on the Official MySQL Documentation. Additionally, you can read about more MySQL String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *