MySQL RIGHT() function
RIGHT() function
MySQL RIGHT() extracts a specified number of characters from the right side of a string.
Syntax:
RIGHT(str, len)
Arguments
Name | Description |
---|---|
str | A string from whose right side a number of characters are to be extracted. |
len | An integer indicating the number of characters to be extracted from str. |
Note: This function is multi-byte safe.
Syntax Diagram:

MySQL Version: 5.6
Video Presentation
Pictorial Presentation
Example of MySQL RIGHT() function
The following MySQL statement returns the rightmost 8 characters for the given string ‘w3resource’.
Code:
SELECT RIGHT('w3resource',8);
Sample Output:
mysql> SELECT RIGHT('w3resource',8); +-----------------------+ | RIGHT('w3resource',8) | +-----------------------+ | resource | +-----------------------+ 1 row in set (0.00 sec)
Example of MySQL RIGHT() function using table
The following MySQL statement returns the rightmost 7 characters from the column ‘aut_name’ in the table author for those rows, which have the column value of ‘country’ is ‘UK’.
Code:
SELECT aut_name,
RIGHT(aut_name,7)
FROM author
WHERE country='UK';
Sample table: author
Sample Output:
mysql> SELECT aut_name, -> RIGHT(aut_name,7) -> FROM author -> WHERE country='UK'; +-----------------+-------------------+ | aut_name | RIGHT(aut_name,7) | +-----------------+-------------------+ | William Norton | Norton | | William Anthony | Anthony | | Piers Gibson | Gibson | | C. J. Wilde | . 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.0">
<title>example-right-function - php mysql examples | w3resource</title>
<meta name="description" content="example-right-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 containg last seven characters of the Author's name:</h2>
<table class='table table-bordered'>
<tr>
<th>Author's name</th><th>Last seven characters of the Author's name</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,RIGHT(aut_name,7)
FROM author
WHERE country="UK"') as $row) {
echo "<tr>";
echo "<td>" . $row['aut_name'] . "</td>";
echo "<td>" . $row['RIGHT(aut_name,7)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
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-right-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,RIGHT(aut_name,7) FROM author WHERE country='UK'";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Author's name</td>
<td>Last seven characters of the Author's name</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("aut_name")%></TD>
<TD><%=rs.getString("RIGHT(aut_name,7)")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Cant connect to database.");
}
%>
</body>
</html>
Online Practice Editor:
All String Functions
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises