SQL REVERSE 字符串函数

一、SQL REVERSE 字符串函数 语法

SQL 中的 REVERSE 字符串函数以相反的顺序返回字符串。它在查询输出的第一个位置显示字符串的最后一个字符,在最后一个位置显示第一个字符。

语法1:此语法使用带有 SQL 表的列名的 REVERSE 函数:

SELECT REVERSE (Column_Name) AS Alias_Name FROM Table_Name;

在语法中,我们必须指定要运行 REVERSE 函数的列的名称。

语法2:此语法使用带有字符串的 REVERSE 函数:

SELECT REVERSE (Original_String);  

二、SQL REVERSE 字符串函数 示例

示例 1:以下 SELECT 查询反转给定的字符串:

SELECT REVERSE (  '          YIIDIAN');  

输出结果为:

'NAIDIIY          '

示例 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)  
);   

以下 CREATE 语句创建Employee_Grade表:

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 查询对上述 Employee_Grade 表的 First_Name 列使用 REVERSE 函数:

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

热门文章

优秀文章