REVERSE function in SQL
In SQL, reversing a string can be accomplished using the REVERSE() function. This function takes a string as an argument and returns the string with its characters in reverse order.
SQL
SELECT REVERSE(string_column) FROM table_name;
Example: Create the Employees table.
SQL
CREATE TABLE Employees ( EmployeeID INT, Name VARCHAR(20), Age int, Salary VARCHAR(10), Department VARCHAR(25), Gender VARCHAR(1) );
Insert the data in the Employees table.
SQL
INSERT INTO Employees VALUES (1, 'Ashish', 24, '25000', 'Electrical', 'M'), (2, 'Kirshan', 32, '30000', 'Electrical', 'M'), (3, 'Anjali', 20, '25000', 'Electronics', 'F'), (4, 'Manjulika', 30, '15000', 'Electrical', 'F'), (5, 'Katrina', 37, '18000', 'Computer', 'F'), (6, 'Esha', 37, '20000', 'Computer', 'F'), (7, 'Ankita', 20, '9000', 'Electronics', 'M'), (8, 'Meenakshi', 30, '15000', 'Computer', 'F'), (9, 'Alia', 37, '16000', 'Electronics', 'F');
Note: In SQL, string literals should be enclosed in single quotes (' ').
By using the Create table and insert statement we reach to the following table in our database:
SQL
Select * from Employees;
Table: Employees
EmployeeID | Name | Age | Salary | Department | Gender |
---|---|---|---|---|---|
1 | Ashish | 24 | 25000 | Electrical | M |
2 | Kirshan | 32 | 30000 | Electrical | M |
3 | Anjali | 20 | 25000 | Electronics | F |
4 | Manjulika | 30 | 15000 | Electrical | F |
5 | Katrina | 37 | 18000 | Computer | F |
6 | Esha | 37 | 20000 | Computer | F |
7 | Ankita | 20 | 9000 | Electronics | M |
8 | Meenakshi | 30 | 15000 | Computer | F |
9 | Alia | 37 | 16000 | Electronics | F |
Query to get the reverse of each Name
SQL
SELECT EmployeeID, Name, Reverse(Name) as ReversedNames FROM employees;
The result of the above query is shown below:
EmployeeID | Name | ReversedNames |
---|---|---|
1 | Ashish | hsihsA |
2 | Kirshan | nahsriK |
3 | Anjali | ilajnA |
4 | Manjulika | akilujnaM |
5 | Katrina | anirtaK |
6 | Esha | ahsE |
7 | Ankita | atiknA |
8 | Meenakshi | ihskaneeM |
9 | Alia | ailA |