SQL SUBSTRING函数
结构化查询语言(SQL)中的SUBSTRING字符串函数显示原始字符串的特定索引值及其之后的字符或子字符串。我们还可以将SUBSTRING函数与SQL表一起使用。
SUBSTRING字符串函数的语法
语法1: 此语法将SUBSTRING函数与SQL表的列名一起使用:
SELECT SUBSTRING(Column_Name, Starting_Index_value, Length_of_string) AS Alias_Name FROM Table_Name;
在substring函数的语法中,我们必须定义我们想要执行它的列的名称。在这里,Length_of_string参数是可选的。如果省略它,则该函数从起始索引值开始显示整个字符串。
语法2: 此语法使用带有字符串的SUBSTRING函数:
SELECT SUBSTRING(Original_String, Starting_Index_value, Length_of_string);
语法 3: 此语法使用SUBSTRING函数与单个字符:
SELECT SUBSTRING(String, Starting_Index_value, 1);
示例SUBSTRING字符串函数
示例1: 下面的SELECT查询显示给定字符串从第17位开始的字符。
SELECT SUBSTRING('JavaTpoint is a website for professionals', 17, 25) AS substring_17_25;
这个SQL查询返回字符串中第17个位置之后的带有空格的25个字符。 th 位置。
输出:
substring_17_25 |
---|
Website for professionals |
示例2: 下面的SELECT查询显示了给定字符串中从第-16个位置开始的字符:
SELECT SUBSTRING( 'JavaTpoint is a website for professionals', -16, 5) AS substring_-17_5;
这条SQL查询显示字符串中倒数第16个位置后的五个字符。 th 位置。
输出:
substring_-16_5 |
---|
or pr |
示例3: 以下SELECT查询显示了字符串中第5个位置起的所有字符。
SELECT SUBSTRING( 'New Delhi IS the Capital OF India', 5) AS substring_5;
输出:
substring_5 |
---|
Delhi IS the Capital OF India |
示例 4: 下面的SELECT查询显示字符串的第6个位置的单个字符。
SELECT SUBSTRING( 'JavaTpoint', 6, 1) AS substring_6_1;
输出:
substring_6_1 |
---|
o |
示例5:此示例使用SQL表中的SUBSTRING函数
在此示例中,我们将创建一个新的SQL表,我们想要对其执行SUBSTRING函数。
在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)
);
以下是创建 Student_Grade 表的CREATE语句:
CREATE TABLE Student_Grade
(
Roll_No INT PRIMARY KEY,
First_Name VARCHAR (100),
Last_Name VARCHAR (100),
First_City Varchar(120),
Second_City Varchar(120),
New_City Varchar(120),
Hindi_Marks INT,
Maths_Marks INT,
Grade Varchar (80)
);
以下的INSERT查询将学生的成绩和分数记录插入 Student_Grade 表中:
INSERT INTO Student_Grade (Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) VALUES (10, Aman, Sharma, Lucknow Chandigarh, Ghaziabad, 88, 95, A2);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES ( 02, Vishal, Sharma, Chandigarh, Ghaziabad, Delhi, 95, 82, A1 );
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (07, Raj, Gupta, Delhi, Ghaziabad, Lucknow, 91, 95, A1);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (04, Yash, Singhania, Ghaziabad, Delhi, Lucknow, 85, 82, A2);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (11, Vinay, Roy, Delhi, Kanpur, Ghaziabad, 95, 97, A1);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (16, Manoj, Gupta, Ghaziabad, Meerut, Chandigarh, 95, 90, B1);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (19, Ram, Gupta, Lucknow, Ghaziabad, Chandigarh, 89, 95, A2);
以下SELECT语句显示了上述插入的记录,来自 Student_Grade 表:
SELECT * FROM Student_Grade;
Roll_No | First_Name | Last_Name | First_City | Second_City | New_City | Hindi_Marks | Maths_Marks | Grade |
---|---|---|---|---|---|---|---|---|
10 | Aman | Sharma | Lucknow | Chandigarh | Ghaziabad | 88 | 95 | A2 |
02 | Vishal | Sharma | Chandigarh | Ghaziabad | Ghaziabad | 95 | 82 | A1 |
07 | Raj | Gupta | Delhi | Ghaziabad | Lucknow | 91 | 95 | A1 |
04 | Yash | Singhania | Ghaziabad | Delhi | Lucknow | 85 | 82 | A2 |
11 | Vinay | Roy | Delhi | Kanpur | Ghaziabad | 95 | 97 | A1 |
16 | Manoj | Gupta | Ghaziabad | Meerut | Chandigarh | 95 | 90 | B1 |
19 | Ram | Gupta | Lucknow | Ghaziabad | Chandigarh | 89 | 95 | A2 |
查询1: 以下SELECT查询使用上述Student_Grade表的Last_Name列与SUBSTRING函数:
SELECT Last_Name, SUBSTRING(Last_Name, 2, 4) AS SUBSTRING_2_4 FROM Student_Grade;
这个SQL语句显示了每个学生的姓氏中从第2个字符开始的四个字符。
输出结果:
Last_Name | SUBSTRING_2_4 |
---|---|
Sharma | harm |
Sharma | harm |
Gupta | upta |
Singhania | ingh |
Roy | oy |
Gupta | upta |
Gupta | upta |
查询2: 以下SELECT查询使用上述Student_Grade表的Last_Name列的SUBSTRING函数:
SELECT Last_Name, SUBSTRING(Last_Name, -3, 2) AS SUBSTRING_-3_2 FROM Student_Grade;
这个SQL语句显示了每个学生姓氏的倒数第三个位置的两个字符。
输出:
Last_Name | SUBSTRING_-3_2 |
---|---|
Sharma | rm |
Sharma | rm |
Gupta | pt |
Singhania | ni |
Roy | Ro |
Gupta | pt |
Gupta | pt |