SQL SUBSTRING_INDEX 函数

一、SQL SUBSTRING_INDEX 函数 语法

SUBSTRING_INDEX 字符串函数显示给定字符串中特定符号之前的所有字符。

语法1:此语法使用 SUBSTRING_INDEX 和 SQL 表的列名:

SELECT SUBSTRING_INDEX(Column_Name, symbol, starting_position) AS Alias_Name FROM Table_Name; 

在语法中,我们必须指定要在其上执行 SUBSTRING_INDEX 字符串函数的列的名称。

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

SELECT SUBSTRING_INDEX(Original_String, symbol, starting_position);  

二、SQL SUBSTRING_INDEX 函数 示例

示例 1:以下 SELECT 查询显示原始单词 JAVA^TPOINT 中给定符号之前的四个字符:

SELECT SUBSTRING_INDEX( 'YIID^IAN', '^', 1) AS SUBSTRING_INDEX_4_characters;

输出结果为:

SUBSTRING_INDEX_4_characters
YIID

示例 2:以下 SELECT 查询显示给定字符串中的 SUBSTRING_INDEX 20 个字符:

SELECT SUBSTRING_INDEX(  'YIIDIAN is a. good website', '.', 1 ) AS SUBSTRING_INDEX_before.symbol;

输出结果为:

SUBSTRING_INDEX_before.symbol
YIIDIAN is a

示例 3:以下 SELECT 查询显示给定 'congratulations' 单词中从第三个位置到给定符号的 5 个字符:

SELECT SUBSTRING_INDEX(  'CONGRAT@ULAT.ONS', '@', 3) AS SUBSTRING_INDEX_before@symbol;  

输出结果为:

SUBSTRING_INDEX_before@symbol
NGRAT

示例 4:以下 SELECT 查询显示给定字符串中从第五个位置到给定符号的 20 个字符:

SELECT SUBSTRING_INDEX('New Delhi is the capital# of India', '#', 5) AS SUBSTRING_INDEX_5_characters; 

输出结果为:

SUBSTRING_INDEX_before#symbol
Delhi is the capital

示例 5:此示例对结构化查询语言中的表使用 SUBSTRING_INDEX 函数。

要理解 SQL 的 SUBSTRING_INDEX 函数,我们必须首先使用 CREATE 语句创建 SQL 表。在 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, Stude.nt_Marks) VALUES (4001, Ama.n, Roy, Sha#rma, 4, Chandigarh, Pun#jab, 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, Vish.al, Gurr, Sh#arma, 8, Murthal, Harya#na, 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, Gupt#a, 6, Ghaziabad, Uttar Pr#adesh, 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, Yas.h, Chopra, Singh#ania, 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, Vin.ay, 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, Man.oj, singhania, Gup#ta, 5, Ghaziabad, Uttar Pr#adesh, 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, Ra.m, Raheem, Gupt#a, 9, Lucknow, Uttar Pra#desh, 89);  

以下 SELECT 语句显示上述Student_Marks表的插入记录:

SELECT * FROM Student_Marks; 

输出结果为:

Schoolboy_Id Schoolboy_First_Name Schoolboy_Middle_Name Schoolboy_Last_Name Schoolboy_Class Schoolboy_City Schoolboy_State Schoolboy_Marks
4001 Ama.n Roy Sha#rma 4 Chandigarh Pun#jab 88
4002 Vish.al Gurr Sh#arma 8 Murthal Harya#na 95
4007 Raj. singhania Gupt#a 6 Ghaziabad Uttar Pr#adesh 91
4004 Yas.h Chopra Singh#ania 9 Jaipur Rajasthan# 85
4011 Vin.ay Sharma Roy# 8 Chandigarh Punjab# 94
4006 Man.oj Singhania Gup#ta 5 Ghaziabad Uttar Pra#desh 83
4010 Ra.m Raheem Gupt#a 9 Lucknow Uttar Pr#adesh 89

查询 1:以下 SELECT 查询将 SUBSTRING_INDEX 函数与上述 Student_Marks 表的 Student_First_Name 列一起使用:

SELECT Student_First_Name, SUBSTRING_INDEX(Student_First_Name, ., 1) AS SUBSTRING_INDEX_before.symbol FROM Student_Marks;  

此 SQL 语句显示每个学生名字中从第一个位置到 # 符号的字符。

输出结果为:

Student_First_Name SUBSTRING_INDEX_before.symbol
Ama.n Ama
Vish.al Vish
Raj. Raj
Yas.h Yas
Vin.ay Vin
Man.oj Man
Ra.m Ra

查询 2:以下 SELECT 查询将 SUBSTRING_INDEX 函数与上述 Student_Marks 表的 Student_Last_Name 列一起使用:

SELECT Student_Last_Name, SUBSTRING_INDEX(Student_Last_Name, #, 1) AS SUBSTRING_INDEX_before#symbol FROM Student_Marks;  

此 SQL 语句显示每个学生的姓氏中从第一个位置到 # 符号的字符。

输出结果为:

Student_Last_Name SUBSTRING_INDEX_before#symbol
Sha#rma Sha
Sh#arma Sh
Gupt#a Gupt
Singh#ania Singh
Roy# Roy
Gup#ta Gup
Gupt#a Gupt

查询 3:以下 SELECT 查询将 SUBSTRING_INDEX 函数与上述 Student_Marks 表的 Student_Address 列一起使用:

SELECT Student_State, SUBSTRING_INDEX(Student_State, @, 2) AS SUBSTRING_INDEX_@symbol FROM Student_Marks;  

该 SQL 语句显示每个学生状态中从第二个位置到给定符号的字符。

输出结果为:

Student_State SUBSTRING_INDEX_@symbol
Pun@jab un
Harya@na arya
Uttar Pr@adesh ttar Pr
Rajasthan@ ajasthan
Punjab@ unjab
Uttar Pra@desh ttar Pra
Uttar Pr@adesh ttar Pr

热门文章

优秀文章