Javatpoint Logo
Javatpoint Logo

POSITION Function in SQL

The POSITION string function in Structured Query Language returns the position of the first occurrence of the given character or substring in the original string. If Sub-string is omitted in the original string, the POSITION function returns 0.

Syntax of POSITION String Function

Syntax1: This syntax uses the POSITION 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 character's position.

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

Syntax2: This syntax uses the POSITION function with the individual character:

Examples of POSITION String function

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

Output:

POSITION_S
1

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

Output:

POSITION_DELHI
5

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

Output:

POSITION_Manufacturing_company
20

Example 5: This example uses the POSITION 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 POSITION function with the First_Name column of the above Student_Grade table:

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

Output:

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

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

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

Output:

Last_Name POSITION_S
Sharma 1
Sharma 1
Gupta 0
Singhania 1
Roy 0
Gupta 0
Gupta 0

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

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

Output:

First_City POSITION_ow
Lucknow 6
Chandigarh 0
Delhi 0
Ghaziabad 0
Delhi 0
Ghaziabad 0
Lucknow 6

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

Output:

Second_City POSITION(z IN Second_City) New_City POSITION(a IN New_City)
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






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