SQL PATINDEX函数

SQL PATINDEX函数

SQL中的PATINDEX字符串函数返回原始字符串中指定模式的位置。如果原始字符串中省略了子字符串,则PATINDEX函数返回0。原始字符串的第一个位置表示为1。

PATINDEX字符串函数的语法

语法1: 此语法使用带有SQL表列名的PATINDEX函数:

SELECT PATINDEX(Pattern, Column_Name) AS Alias_Name FROM Table_Name;

在语法中,我们必须指定要在其中查找字符串位置的列的名称。

语法2: 此语法使用PATINDEX函数与字符串:

SELECT PATINDEX(Pattern, Original_String) AS Alias_Name;

PATINDEX字符串函数示例

示例1: 以下SELECT查询显示了原始字符串中字符S的PATINDEX:

SELECT PATINDEX(  '%S%', 'SUSTAINABLE')AS PATINDEX_S;

输出:

PATINDEX_S   
---  
1   

示例2: 下面的SELECT查询显示了给定字符串中DELHI单词的PATINDEX:

SELECT PATINDEX( '%DELHI%', 'NEW DELHI') AS PATINDEX_DELHI;

输出:

PATINDEX_DELHI   
---  
5   

示例3: 下面的SELECT查询在原始字符串中返回“制造公司”字符串的PATINDEX:

SELECT PATINDEX( '%Manufacturing Company%', 'Honda is an indian Manufacturing company') AS PATINDEX_Manufacturing_company;

输出:

PATINDEX_Manufacturing_company   
---  
20   

示例4:这个例子使用了带有结构化查询语言中的表的PATINDEX函数。

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

下面的CREATE语句创建了 Student_Grade 表:

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查询使用了上述学生成绩表的First_Name列的PATINDEX函数:

SELECT First_Name, PATINDEX('%a%', First_Name) AS PATINDEX_a FROM Student_Grade;

这个SQL语句显示了每个学生名字的第一个字符中’a’的PATINDEX值。

输出结果

First_Name PATINDEX_a
Aman 1
Vishal 5
Raj 2
Yash 2
Vinay 4
Manoj 2
Ram 2

查询2: 以下SELECT查询使用了上面的Student_Grade表的Last_Name列的PATINDEX函数:

SELECT Last_Name, PATINDEX('%S%', Last_Name) AS PATINDEX_S FROM Student_Grade;

这个SQL语句显示了每个学生姓氏中’S’字符的PATINDEX。

输出:

Last_Name PATINDEX_S
Sharma 1
Sharma 1
Gupta 0
Singhania 1
Roy 0
Gupta 0
Gupta 0

查询 3: 下面的 SELECT 查询使用了上面 Student_Grade 表中的 First_City 列的 PATINDEX 函数:

SELECT First_City, PATINDEX('%ow%', First_City) AS PATINDEX_ow FROM Student_Grade;

该SQL语句显示了每个学生的第一个城市中’ow’字符的PATINDEX。

输出:

First_City PATINDEX_ow
Lucknow 6
Chandigarh 0
Delhi 0
Ghaziabad 0
Delhi 0
Ghaziabad 0
Lucknow 6

查询4: 以下SELECT查询使用了上面的Student_Grade表中的Second_City和New_City列的PATINDEX函数:

SELECT Second_City, PATINDEX('%z%', Second_City) AS PATINDEX_z, New_City, PATINDEX('%a%', New_City) AS PATINDEX_a FROM Student_Grade;

输出:

Second_City PATINDEX_z New_City PATINDEX_a
Chandigarh 0 Ghaziabad 3
Ghaziabad 4 Ghaziabad 3
Ghaziabad 4 Lucknow 0
Delhi 0 Lucknow 0
Kanpur 0 Ghaziabad 3
Meerut 0 Chandigarh 3
Ghaziabad 4 Chandigarh 3

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程