SQL SUBSTRING_INDEX函数

SQL SUBSTRING_INDEX函数

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

SUBSTRING_INDEX字符串函数的语法

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

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);

SUBSTRING_INDEX字符串函数示例

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

SELECT SUBSTRING_INDEX( ‘JAVA^TPOINT’, ‘^’, 1) AS SUBSTRING_INDEX_4_characters;

输出:

SUBSTRING_INDEX_4_characters

JAVA

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

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

输出:

SUBSTRING_INDEX_before.symbol

JAVATPOINT 是一个

例子 3: 下面的 SELECT 查询展示了给定符号在给定的 ‘congratulations’ 单词中从第三个位置开始的5个字符:

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

输出:

SUBSTRING_INDEX符号之前

NGRAT

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

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

输出:

SUBSTRING_INDEX_before#symbol

德里是首都

示例6:此示例使用SQL的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查询使用了上面Student_Marks表中的Student_First_Name列的SUBSTRING_INDEX函数:

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查询使用上面Student_Marks表的Student_Last_Name列的SUBSTRING_INDEX函数:

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查询使用了上面的学生成绩表中的Student_Address列的SUBSTRING_INDEX函数:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程