w3resource

MySQL RPAD() function

RPAD() function

MySQL RPAD() function pads strings from right. The actual string which is to be padded, the length of the string returned after padding and string which is used for padding - all these are passed as arguments.

This function is useful in -

  • String padding: It allows us to right-pad a string with a specific character or a set of characters.
  • Data presentation: RPAD() can format or present data in a desired format. For example, wecan use it to display numbers with trailing zeros or align text in columns.

Syntax:

RPAD(str, len, padstr)

Arguments:

Name Description
str The actual string which is to be padded.
len Length of the string returned after padding.
padstr String which will be used for padding.

Syntax Diagram:

MySQL RPAD() Function - Syntax Diagram

MySQL Version: 8.0

Pictorial Presentation:

MySQL RPAD function

Example : MySQL RPAD() function

The following MySQL statement returns a string after right padding with the string ‘*’ to a length of 15 characters.

Code:

SELECT RPAD('w3resource',15,'*'); 

Output:

mysql> SELECT RPAD('w3resource',15,'*'); 
+---------------------------+
| RPAD('w3resource',15,'*') |
+---------------------------+
| w3resource*****           | 
+---------------------------+
1 row in set (0.00 sec)

Example of MySQL RPAD() function specifying shortened length

The following MySQL statement returns a string which is shortened because the string is longer than the total length mentioned in the second argument.

Code:

SELECT RPAD('w3resource',6,'*'); 

Output:

mysql> SELECT RPAD('w3resource',6,'*'); 
+--------------------------+
| RPAD('w3resource',6,'*') |
+--------------------------+
| w3reso                   | 
+--------------------------+
1 row in set (0.00 sec)

Example of MySQL RPAD() function to right padding

The above MySQL statement returns the column aut_name and the aut_name with right padding by the character ‘*’ to the length of 25 characters from the table author for those rows which have the column value of country as ‘UK’.

Code:

SELECT aut_name,RPAD(aut_name,25,'*')
FROM author 
WHERE country='UK';
              

Sample table: author


Output:

mysql> SELECT aut_name,RPAD(aut_name,25,'*')
    -> FROM author 
    -> WHERE country='UK';
+-----------------+---------------------------+
| aut_name        | RPAD(aut_name,25,'*')     |
+-----------------+---------------------------+
| William Norton  | William Norton*********** | 
| William Anthony | William Anthony********** | 
| Piers Gibson    | Piers Gibson************* | 
| C. J. Wilde     | C. J. Wilde************** | 
+-----------------+---------------------------+
4 rows in set (0.00 sec)

Video Presentation:

All String Functions (Slides presentation)

Previous: NOT RLIKE
Next: RTRIM



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mysql/string-functions/mysql-rpad-function.php