SQL REVERSE字符串函数
SQL中的REVERSE字符串函数以相反的顺序返回字符串。它在查询的输出中将字符串的最后一个字符显示在第一个位置,将第一个字符显示在最后一个位置。
REVERSE字符串函数的语法
语法1: 这个语法使用SQL表的列名与REVERSE函数一起使用:
SELECT REVERSE (Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要对其运行REVERSE函数的列的名称。
语法2: 这个语法使用REVERSE函数与字符串:
SELECT REVERSE (Original_String);
REVERSE字符串函数的示例
示例1: 以下SELECT查询将反转给定的字符串:
SELECT REVERSE ( ' JAVATPOINT');
输出:
'TNIOPTAVAJ '
示例 2: SELECT查询将给定的字符串按照相反的顺序显示:
SELECT REVERSE ( 'NEW DELHI IS THE CAPITAL OF INDIA');
输出:
'AIDNI FO LATIPAC EHT SI IHLED WEN'
示例3: 下面的SELECT查询以相反的方式显示数字字符串:
SELECT REVERSE ( '578442297425');
输出:
524792244875
示例4:此示例使用带有结构化查询语言中的表的REVERSE函数。
首先,我们需要创建新的SQL表,这有助于理解REVERSE字符串函数。在SQL数据库中创建新表的语法如下所示:
CREATE TABLE table_name
(
First_Column_of_table Data Type (character_size of First Column),
Second_Column_of_table Data Type (character_size of the Second column ),
Third_Column_of_table Data Type (character_size of the Third column),
...
Last_Column_of_table Data Type (character_size of the Last column)
);
以下是创建表 Employee_Grade 的CREATE语句:
CREATE TABLE Employee_Grade
(
Employee_ID INT PRIMARY KEY,
First_Name VARCHAR (100),
Last_Name VARCHAR (100),
First_City Varchar(120),
Second_City Varchar(120),
New_City Varchar(120),
Attendance_Remarks INT,
Work_Remarks INT,
Grade Varchar (80)
);
下面的INSERT查询将员工的等级和备注记录插入到 Employee_Grade 表中:
INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (10, Ramesh, Sharma, Lucknow Aurangabad, Ghaziabad, 88, 95, A2);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES ( 02, Yadu, Sharma, Aurangabad, Ghaziabad, Noida, 95, 82, A1 );
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (07, Vijay, Ramna, Noida, Ghaziabad, Lucknow, 91, 95, A1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (04, Bhanu, Rangopalr, Ghaziabad, Noida, Lucknow, 85, 82, A2);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (11, Harry, Roy, Noida, Kanpur, Ghaziabad, 95, 97, A1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (16, Akash, Ramna, Ghaziabad, Meerut, Aurangabad, 95, 90, B1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (19, Ram, Ramna, Lucknow, Ghaziabad, Aurangabad, 89, 95, A2);
下面的SELECT语句显示了上面插入的记录,来自以下 Employee_Grade 表:
SELECT * FROM Employee_Grade;
Employee_ID | First_Name | Last_Name | First_City | Second_City | New_City | Attendance_Remarks | Work_Remarks | Grade |
---|---|---|---|---|---|---|---|---|
10 | Ramesh | Sharma | Lucknow | Aurangabad | Ghaziabad | 88 | 95 | A2 |
02 | Yadu | Sharma | Aurangabad | Ghaziabad | Ghaziabad | 95 | 82 | A1 |
07 | Vijay | Ramna | Noida | Ghaziabad | Lucknow | 91 | 95 | A1 |
04 | Bhanu | Rangopalr | Ghaziabad | Noida | Lucknow | 85 | 82 | A2 |
11 | Harry | Roy | Noida | Kanpur | Ghaziabad | 95 | 97 | A1 |
16 | Akash | Ramna | Ghaziabad | Meerut | Aurangabad | 95 | 90 | B1 |
19 | Ram | Ramna | Lucknow | Ghaziabad | Aurangabad | 89 | 95 | A2 |
查询1: 下面的SELECT查询使用REVERSE函数与上面的Employee_Grade表的First_Name列:
SELECT First_Name, REVERSE(First_Name) AS REVERSE_FirstName FROM Employee_Grade;
这个语句显示了姓氏中所有值的倒序。
输出结果:
First_Name | REVERSE_FirstName |
---|---|
Ramesh | hsemaR |
Yadu | udaY |
Vijay | yajiV |
Bhanu | unahB |
Harry | yrraH |
Akash | hsakA |
Ram | maR |
查询2: 以下SELECT查询使用了上述Employee_Grade表的Last_Name列的REVERSE函数:
SELECT Last_Name, REVERSE(Last_Name) AS REVERSE_LastName FROM Employee_Grade;
这个语句以相反的顺序显示了所有姓氏的值。
输出:
Last_Name | REVERSE_LastName |
---|---|
Sharma | amrahS |
Sharma | amrahS |
Ramna | anmaR |
Rangopalr | rlapognaR |
Roy | yoR |
Ramna | anmaR |
Ramna | anmaR |
查询3: 下面的SELECT查询使用了上面Employee_Grade表中的First_City和New_City列的REVERSE函数:
SELECT First_City, REVERSE(First_City) AS REVERSE_FirstCity, New_City, REVERSE(New_City) AS REVERSE_NewCity FROM Employee_Grade;
这个SQL语句显示了First_City和New_City的所有城市,按相反的顺序排列。
输出结果:
First_City | REVERSE_FirstCity | New_City | REVERSE_NewCity |
---|---|---|---|
Lucknow | wonkcuL | Ghaziabad | dabaizahG |
Aurangabad | dabagnaruA | Ghaziabad | dabaizahG |
Noida | adioN | Lucknow | wonkcuL |
Ghaziabad | dabaizahG | Lucknow | wonkcuL |
Noida | adioN | Ghaziabad | dabaizahG |
Ghaziabad | dabaizahG | Aurangabad | dabagnaruA |
Lucknow | wonkcuL | Aurangabad | dabagnaruA |