SQL SUBSTR 函数
一、SQL SUBSTR 函数 语法
SQL语言中的 SUBSTR 字符串函数显示来自原始字符串的特定索引值的字符或子字符串。SQL 还允许您对表使用 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);
语法3:此语法使用带有单个字符的 SUBSTR 函数:
SELECT SUBSTR(String, Starting_Index_value, 1);
二、SQL SUBSTR 函数 示例
示例 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位的字符:
SELECT SUBSTR( 'Yiidian is a website for professionals', -17, 5);
此 SQL 查询显示字符串最后 17个位置的五个字符。
输出结果为:
website for professionals
示例 3:以下 SELECT 查询显示字符串第 5个位置的所有字符。
SELECT SUBSTR( 'New Delhi IS the Capital OF India', 5);
输出结果为:
Delhi IS the Capital OF India
示例 4:以下 SELECT 查询显示字符串第 6个位置的单个字符。
SELECT SUBSTR( 'Yiidian', 8, 1);
输出结果为:
a
示例 5:此示例将 SUBSTR 函数与 SQL 表一起使用
在此示例中,我们将创建一个要在其上执行 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 查询将 SUBSTR 函数与上述 Student_Marks 表的 Student_Last_Name 列一起使用:
SELECT Student_Last_Name, SUBSTR(Student_Last_Name, 2, 4) AS SUBSTR_2_4 FROM Student_Marks;
此 SQL 语句显示每个学生的 Last name 的第二个位置之后的四个字符。
输出结果为:
Student_Last_Name | SUBSTR_2_4 |
---|---|
Sharma | harm |
Sharma | harm |
Gupta | upta |
Singhania | ingh |
Roy | oy |
Gupta | upta |
Gupta | upta |
查询 2:以下 SELECT 查询将 SUBSTR 函数与上述 Student_Marks 表的 Student_Last_Name 列一起使用:
SELECT Student_Last_Name, SUBSTR(Student_Last_Name, -3, 2) AS SUBSTR_-3_2 FROM Student_Marks;
此 SQL 语句显示每个学生的 Last name 倒数第三个位置的两个字符。
输出结果为:
Student_Last_Name | SUBSTR_-3_2 |
---|---|
Sharma | rm |
Sharma | rm |
Gupta | pt |
Singhania | ni |
Roy | Ro |
Gupta | pt |
Gupta | pt |
热门文章
优秀文章