
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we reverse a MySQL string connected by the dash?
MySQL has function name REVERSE() with the help of which we can reverse the string. But suppose if we want to reverse the string connected by dash then by using REVERSE() function will not give appropriate result as shown in the following example:
mysql> Select REVERSE('AB-CD-EF'); +---------------------+ | REVERSE('AB-CD-EF') | +---------------------+ | FE-DC-BA | +---------------------+ 1 row in set (0.00 sec)
The appropriate result would be ‘EF-CD-AB’ and for getting such output we can use SUBSTRING_INDEX() function along with Instr() function. It is demonstrated as follows:
mysql> Select CONCAT(SUBSTRING_INDEX('AB-CD-EF','-',-1), '-', substr('AB-CD-EF',instr('AB-CD-EF',"-")+1, instr('AB-CD-EF',"-")),LEFT('AB-CD-EF',LOCATE('-','AB-CD-EF') -1))As 'Reversed'; +-----------+ | Reversed | +-----------+ | EF-CD-AB | +-----------+ 1 row in set (0.00 sec)
- Related Articles
- In MySQL, how can we pad a string with another string?
- How can we check that by default MySQL CHAR() function returns a binary string?
- How can I reverse a string in Java?
- How can we split the name string into two parts by using MySQL SUBSTRING_INDEX() function?
- How can we split the name string into three parts by using MySQL SUBSTRING_INDEX() function?
- How can we retrieve the length of a specified string in MySQL?
- How can we extract a substring from a string in MySQL?
- How can we produce a string, other than default binary string, in a given character set by MySQL CHAR() function?
- How can we create a MySQL user account by omitting the hostname?
- How can we use MySQL REVERSE() function on column’s data along with WHERE clause?
- How can we delete a MySQL database by using PHP script?
- How can we select a MySQL database by using PHP script?
- How can we create a MySQL table by using PHP script?
- How can we create a MySQL view with GROUP BY clause?
- How can we stuff a string with another one using MySQL functions?

Advertisements