Javatpoint Logo
Javatpoint Logo

PATINDEX Function in SQL

The PATINDEX string function in Structured Query Language returns the position of the specified pattern in the original string. If Sub-string is omitted in the original string, the PATINDEX function returns 0. The first position of the original string is indicated as 1.

Syntax of PATINDEX String Function

Syntax1: This syntax uses the PATINDEX function with the column name of the SQL table:

In the syntax, we have to specify the name of that column on which we want to find the position of string.

Syntax2: This syntax uses the PATINDEX function with the string:

Examples of PATINDEX String function

Example 1: The following SELECT query shows the PATINDEX of the character S in the original string:

Output:

PATINDEX_S
1

Example 2: The following SELECT query shows the PATINDEX of the DELHI word in the given string:

Output:

PATINDEX_DELHI
5

Example 3: The following SELECT query returns the PATINDEX of the 'Manufacturing Company' string in the original string:

Output:

PATINDEX_Manufacturing_company
20

Example 4: This example uses the PATINDEX function with the table in Structured Query Language.

The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Grade table:

The below INSERT queries insert the records of students with grades and marks in the Student_Grade table:

The following SELECT statement displays the inserted records of the above Student_Grade table:


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

Query 1: The following SELECT query uses the PATINDEX function with the First_Name column of the above Student_Grade table:

This SQL statement shows the PATINDEX of 'a' character in the first name of each student.

Output

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

Query 2: The following SELECT query uses the PATINDEX function with the Last_Name column of the above Student_Grade table:

This SQL statement shows the PATINDEX of the 'S' character in the last name of each student.

Output:

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

Query 3: The following SELECT query uses the PATINDEX function with the First_City column of the above Student_Grade table:

This SQL statement shows the PATINDEX of the 'ow' character in the first city of each student.

Output:

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

Query 4: The following SELECT query uses the PATINDEX function with the Second_City and New_City column of the above Student_Grade table:

Output:


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

Next TopicPI Function in SQL





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA