w3resource
MYSQL Tutorial

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.

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: 5.6

Video Presentation

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,'*'); 

Sample 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,'*'); 

Sample 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


Sample 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)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-rpad-function - php mysql examples | w3resource</title>
<meta name="description" content="example-rpad-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>A list of Authors those who belong to UK. Second column shows a string by right padding by the character '*' to length of 25 characters of the Author's name:</h2>
<table class='table table-bordered'>
<tr>
<th>Author's name</th><th>RIGHT(aut_name,7)</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT aut_name,RPAD(aut_name,25,"*") as output
FROM author
WHERE country="UK"') as $row) {
echo "<tr>";
echo "<td>" . $row['aut_name'] . "</td>";
echo "<td>" . $row['output'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-rpad-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT aut_name,RPAD(aut_name,25,'*') as output FROM author WHERE country='UK'";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Author's name</td>
<td>RIGHT(aut_name,7)</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("aut_name")%></TD>
<TD><%=rs.getString("output")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Cant connect to database.");
}
%>
</body>
</html>

All String Functions

MySQL String Functions, slide presentation

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.