w3resource
MYSQL Tutorial

MySQL REVERSE() function

REVERSE() function

MySQL REVERSE() function reverses a string supplied as an argument.

Syntax:

REVERSE(str)

Where str is a string.

Note: This function is multi-byte safe.

Syntax Diagram:

MySQL REVERSE() Function - Syntax Diagram

MySQL Version: 5.6

Video Presentation

Example of MySQL REVERSE() function

The following MySQL statement returns the string given in the argument in reverse order.

Code:

SELECT REVERSE('w3resource');

Sample Output:

mysql> SELECT REVERSE('w3resource');
+-----------------------+
| REVERSE('w3resource') |
+-----------------------+
| ecruoser3w            | 
+-----------------------+
1 row in set (0.02 sec)

Example MySQL REVERSE() function using table

The following MySQL statement returns the column country in reverse order for those rows from the table publisher in which the column value of a country is the ‘USA’.

Code:

SELECT pub_city,country,
REVERSE(country)
FROM publisher 
WHERE country='USA';

Sample table: publisher


Sample Output:

mysql> SELECT pub_city,country,
    -> REVERSE(country)
    -> FROM publisher 
    -> WHERE country='USA';
+----------+---------+------------------+
| pub_city | country | REVERSE(country) |
+----------+---------+------------------+
| New York | USA     | ASU              | 
| Houstan  | USA     | ASU              | 
| New York | USA     | ASU              | 
+----------+---------+------------------+
3 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-reverse-function - php mysql examples | w3resource</title>
<meta name="description" content="example-reverse-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 Publishers those who belong to USA. Second column shows the country of the Publisher. Last column shows 
reverse of the country name:</h2>
<table class='table table-bordered'>
<tr>
<th>Publishers city</th><th>Publishers country</th><th>Reverse of the country 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 pub_city,country,REVERSE(country)
FROM publisher 
WHERE country="USA"') as $row) {
echo "<tr>";
echo "<td>" . $row['pub_city'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['REVERSE(country)'] . "</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-reverse-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 pub_city,country,REVERSE(country) FROM publisher WHERE country='USA'";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Publishers city</td>
<td>Publishers country</td>
<td>Reverse of the country name</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("pub_city")%></TD>
<TD><%=rs.getString("country")%></TD>
<TD><%=rs.getString("REVERSE(country)")%></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