SQL RIGHT函数
RIGHT字符串函数从结构化查询语言中的原始字符串的右侧检索到给定索引值的字符。
RIGHT字符串函数的语法
语法1: 此语法使用SQL表的列名与RIGHT函数一起使用:
SELECT RIGHT(Column_Name, Index_position) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要执行RIGHT字符串函数的列的名称。
语法3: 这个语法使用RIGHT函数和字符串:
SELECT RIGHT(Original_String, Index_position);
RIGHT函数示例
示例1: 以下SELECT查询显示了指定单词右侧的四个字符:
SELECT RIGHT( ' JAVATPOINT', 4) AS Right_4_characters;
输出:
Right_4_characters
OINT’
示例2: 下面的SELECT查询显示了从给定字符串中的右边20个字符:
SELECT RIGHT( 'JAVATPOINT is a good website', 20) AS Right_20_characters;
输出:
Right_20_characters
NT is a good website
示例3: 下面的SELECT查询显示给定句子中的后16个字符:
SELECT RIGHT( 'NEW DELHI IS THE CAPITAL OF INDIA', 16) AS Right_16_characters;
输出:
Right_20_characters
CAPITAL OF INDIA
示例4: 以下SELECT查询显示给定字符串右侧的5个字符:
SELECT RIGHT( '####98221545###', 5) AS Right_5_characters;
输出:
45###
示例 5: 下面的SELECT查询显示了指定字符串右侧的100个字符:
SELECT RIGHT( 'String Functions', 100) AS Right_100_characters;
输出:
Right_100_characters
String Functions
示例6:此示例使用右函数和结构化查询语言中的表。
要理解与SQL中的RIGHT函数,请首先使用CREATE语句创建SQL表。在SQL数据库中创建新表的语法如下:
CREATE TABLE table_name
(
1st_Column Data Type (character_size of 1st Column),
2nd_Column Data Type (character_size of the 2nd column ),
3rd_Column Data Type (character_size of the 3rd column),
...
Nth_Column Data Type (character_size of the Nth column)
);
下面的CREATE语句创建了 Worker_Info 表:
CREATE TABLE Worker_Info
(
Worker_ID INT NOT NULL PRIMARY KEY,
Worker_First_Name VARCHAR (100),
Worker_Last_Name VARCHAR (100),
Worker_Dept_Id INT NOT NULL,
Worker_Address Varchar(120),
Worker_City Varchar (80),
Worker_Salary INT
);
下面的INSERT查询将学院教员的记录插入到 Worker_Info 表中:
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1001, Arush, Sharma, 4001, Aman Vihar, Delhi, 20000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1002, Bulbul, Roy, 4002, Nirman Vihar, Delhi, 38000 );
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1004, Saurabh, Sharma, 4001, Sector 128, Mumbai, 45000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1005, Shivani, Singhania, 4001, Vivek Vihar, Kolkata, 42000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1006, Avinash, Sharma, 4002, Sarvodya Calony, Delhi, 28000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary)VALUES (1007, Shyam, Besas, 4003, Krishna Nagar, Lucknow, 35000);
下面的SELECT语句显示了上述 Worker_Info 表中插入的记录:
SELECT * FROM Worker_Info;
Worker_Id | Worker_First_Name | Worker_Last_Name | Worker_Dept_Id | Worker_Address | Worker_City | Worker_Salary |
---|---|---|---|---|---|---|
1001 | Arush | Sharma | 4001 | Aman Vihar | Delhi | 20000 |
1002 | Bulbul | Roy | 4002 | Nirman Vihar | Delhi | 38000 |
1004 | Saurabh | Roy | 4001 | Sector 128 | Mumbai | 45000 |
1005 | Shivani | Singhania | 4001 | Vivek Vihar | Kolkata | 42000 |
1006 | Avinash | Sharma | 4002 | Sarvodya Calony | Delhi | 28000 |
1007 | Shyam | Besas | 4003 | Krishna Nagar | Lucknow | 35000 |
查询1: 以下SELECT查询使用了上述Worker_Info表的Worker_First_Name列的RIGHT函数:
SELECT Worker_First_Name, RIGHT(Worker_First_Name, 3) AS RIGHT_3FirstName FROM Worker_Info;
该SQL语句显示每个工作者名字的最后三个字符。
输出:
Worker_First_Name | RIGHT_3FirstName |
---|---|
Arush | ush |
Bulbul | bul |
Saurabh | abh |
Shivani | ani |
Avinash | ash |
Shyam | yam |
查询2: 下面的SELECT查询使用了上述Worker_Info表中的Worker_Last_Name列的RIGHT函数:
SELECT Worker_Last_Name, RIGHT(Worker_Last_Name, 2) AS RIGHT_2LastName FROM Worker_Info;
这个SQL语句显示每个工人的姓氏的最后三个字符。
输出结果:
Worker_Last_Name | RIGHT_2LastName |
---|---|
Sharma | ma |
Roy | oy |
Roy | oy |
Singhania | ia |
Sharma | ma |
Besas | as |
查询3: 以下SELECT查询使用上述Worker_Info表的Worker_Address列的RIGHT函数:
SELECT Worker_Address, RIGHT(Worker_Address, 4) AS RIGHT_4Address FROM Worker_Info;
这个SQL语句显示了每个工人地址右边的四个字符。
输出:
Worker_Address | RIGHT_4Address |
---|---|
Aman Vihar | ihar |
Nirman Vihar | ihar |
Sector 128 | 128 |
Vivek Vihar | ihar |
Sarvodya Calony | lony |
Krishna Nagar | agar |
查询4: 以下SELECT查询使用了上面Worker_Info表中的Worker_City列中的RIGHT函数:
SELECT Worker_City, RIGHT(Worker_City, 1) AS RIGHT_1City FROM Worker_Info;
这个SQL语句显示了每个员工所在城市的最后一个字符。
Output:
Worker_City | RIGHT_1City |
---|---|
Delhi | i |
Delhi | i |
Mumbai | i |
Kolkata | a |
Delhi | i |
Lucknow | w |