SQL SUBSTR函数
在结构化查询语言中,SUBSTR字符串函数显示原始字符串的特定索引值的字符或子字符串。SQL还允许您在表格中使用SUBSTR函数。
SUBSTR字符串函数的语法
语法1: 此语法使用SQL表的列名与SUBSTR函数一起使用:
SELECT SUBSTR(Column_Name, Starting_Index_value, Length_of_string) AS Alias_Name FROM Table_Name;
在这个语法中,我们需要定义要执行SUBSTR()函数的那一列的名称。在这里,Length_of_string参数是可选的。如果省略它,那么这个函数将会从起始索引值开始提取整个字符串。
语法2: 这个语法使用了带有字符串的SUBSTR函数:
SELECT SUBSTR(Original_String, Starting_Index_value, Length_of_string);
语法2: 使用SUBSTR函数和一个字符的语法:
SELECT SUBSTR(String, Starting_Index_value, 1);
HTML格式的英文文本翻译成中文;
例子1: 以下的SELECT查询显示了给定字符串的第17位字符。
SELECT SUBSTR( 'JavaTpoint is a website for professionals', 17, 24);
This SQL query returns the 24 characters with spaces after the 17th position in the string.
输出:
website for professionals
示例2: 下面的SELECT查询显示给定字符串中从第-17个th位置开始的字符:
SELECT SUBSTR( 'JavaTpoint is a website for professionals', -17, 5);
这个SQL查询显示了字符串的倒数第17个位置开始的五个字符。 th 位置。
输出:
website for professionals
示例3: 下面的SELECT查询显示了字符串中第5个字符及其之后的所有字符。
SELECT SUBSTR( 'New Delhi IS the Capital OF India', 5);
输出:
Delhi IS the Capital OF India
示例4: 下面的SELECT查询显示了字符串中第8个字符。
SELECT SUBSTR( 'JavaTpoint', 8, 1);
输出:
n
示例5:此示例使用SQL表的SUBSTR函数
在此示例中,我们将创建一个新表,然后对其执行SUBSTR函数。
在此示例中,我们需要通过创建一个新的SQL表来执行Concat()函数。创建新SQL表的语法如下所示:
CREATE TABLE table_name
(
First_Column_of_table Data Type (character_size of 1st Column),
Second_Column_of_table Data Type (character_size of the 2nd column ),
Third_Column_of_table Data Type (character_size of the 3rd column),
...
Last_Column_of_table Data Type (character_size of the Nth column)
);
以下CREATE语句创建了 Student_Marks 表:
CREATE TABLE Student_Marks
(
Student_ID INT NOT NULL PRIMARY KEY,
Student_First_Name VARCHAR (100),
Student_Middle_Name VARCHAR (100),
Student_Last_Name VARCHAR (100),
Student_Class INT NOT NULL,
Student_City Varchar(120),
Student_State Varchar (80),
Student_Marks INT
);
以下INSERT查询将学院教职工的记录插入 Student_Marks 表中:
INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES (4001, Aman, Roy, Sharma, 4, Chandigarh, Punjab, 88);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES ( 4002, Vishal, Gurr, Sharma, 8, Murthal, Haryana, 95 );
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4007, Raj, singhania, Gupta, 6, Ghaziabad, Uttar Pradesh, 91);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4004, Yash, Chopra, Singhania, 9, Jaipur, Rajasthan, 85);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4011, Vinay, Sharma, Roy, 8, Chandigarh, Punjab, 94);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4006, Manoj, singhania, Gupta, 5, Ghaziabad, Uttar Pradesh, 83);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4010, Ram, Raheem, Gupta, 9, Lucknow, Uttar Pradesh, 89);
下面的SELECT语句显示了上述 Student_Marks 表中插入的记录:
SELECT * FROM Student_Marks;
Student_Id | Student_First_Name | Student_Middle_Name | Student_Last_Name | Student_Class | Student_City | Student_State | Student_Marks |
---|---|---|---|---|---|---|---|
4001 | Aman | Roy | Sharma | 4 | Chandigarh | Punjab | 88 |
4002 | Vishal | Gurr | Sharma | 8 | Murthal | Haryana | 95 |
4007 | Raj | singhania | Gupta | 6 | Ghaziabad | Uttar Pradesh | 91 |
4004 | Yash | Chopra | Singhania | 9 | Jaipur | Rajasthan | 85 |
4011 | Vinay | Sharma | Roy | 8 | Chandigarh | Punjab | 94 |
4006 | Manoj | Singhania | Gupta | 5 | Ghaziabad | Uttar Pradesh | 83 |
4010 | Ram | Raheem | Gupta | 9 | Lucknow | Uttar Pradesh | 89 |
查询 1: 以下 SELECT 查询使用上述 Student_Marks 表中的 Student_Last_Name 列的 SUBSTR 函数:
SELECT Student_Last_Name, SUBSTR(Student_Last_Name, 2, 4) AS SUBSTR_2_4 FROM Student_Marks;
这个SQL语句显示了每个学生的姓(Last name)第二个字符后的四个字符。 nd
输出:
Student_Last_Name | SUBSTR_2_4 |
---|---|
Sharma | harm |
Sharma | harm |
Gupta | upta |
Singhania | ingh |
Roy | oy |
Gupta | upta |
Gupta | upta |
查询2: 下面的SELECT查询使用了上述Student_Marks表中的Student_Last_Name列的SUBSTR函数:
SELECT Student_Last_Name, SUBSTR(Student_Last_Name, -3, 2) AS SUBSTR_-3_2 FROM Student_Marks;
这个SQL语句显示了每个学生姓氏倒数第三个位置的两个字符。
输出:
Student_Last_Name | SUBSTR_-3_2 |
---|---|
Sharma | rm |
Sharma | rm |
Gupta | pt |
Singhania | ni |
Roy | Ro |
Gupta | pt |
Gupta | pt |