SUBSTRING_INDEX Function in SQL

The SUBSTRING_INDEX string function shows all the characters before the specific symbol in the given string.

Syntax of SUBSTRING_INDEX String Function

Syntax1: This syntax uses the SUBSTRING_INDEX 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 perform the SUBSTRING_INDEX string function.

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

Examples of SUBSTRING_INDEX String function

Example 1: The following SELECT query shows the four characters before the given symbol in the original word JAVA^TPOINT:

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

Output:

SUBSTRING_INDEX_4_characters
JAVA

Example 2: The following SELECT query shows the SUBSTRING_INDEX 20 characters from the given string:

Output:

SUBSTRING_INDEX_before.symbol
JAVATPOINT is a

Example 3: The following SELECT query shows the 5 characters from the third position to the given symbol in the given 'congratulations' word:

Output:

SUBSTRING_INDEX_before@symbol
NGRAT

Example 4: The following SELECT query shows the 20 characters from the fifth position to the given symbol in the given string:

Output:

SUBSTRING_INDEX_before#symbol
Delhi is the capital

Example 6: This example uses the SUBSTRING_INDEX function with the table in Structured Query Language.

To understand the SUBSTRING_INDEX function with SQL, we have to create the SQL table first using CREATE statement. The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Marks table:

The below INSERT queries insert the records of college Faculties in the Student_Marks table:

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


Schoolboy_IdSchoolboy_First_NameSchoolboy_Middle_NameSchoolboy_Last_NameSchoolboy_ClassSchoolboy_CitySchoolboy_StateSchoolboy_Marks
4001Ama.nRoySha#rma4ChandigarhPun#jab88
4002Vish.alGurrSh#arma8Murthal Harya#na95
4007Raj.singhaniaGupt#a6GhaziabadUttar Pr#adesh91
4004Yas.hChopraSingh#ania9JaipurRajasthan#85
4011Vin.aySharmaRoy#8ChandigarhPunjab#94
4006Man.ojSinghaniaGup#ta5GhaziabadUttar Pra#desh83
4010Ra.mRaheemGupt#a9LucknowUttar Pr#adesh89

Query 1: The following SELECT query uses the SUBSTRING_INDEX function with the Student_First_Name column of the above Student_Marks table:

This SQL statement shows the characters from the first position to the # symbol in the first name of each student.

Output:

Student_First_NameSUBSTRING_INDEX_before.symbol
Ama.nAma
Vish.alVish
Raj.Raj
Yas.hYas
Vin.ayVin
Man.ojMan
Ra.mRa

Query 2: The following SELECT query uses the SUBSTRING_INDEX function with the Student_Last_Name column of the above Student_Marks table:

This SQL statement shows the characters from the first position to the # symbol in the last name of each student.

Output:

Student_Last_NameSUBSTRING_INDEX_before#symbol
Sha#rmaSha
Sh#armaSh
Gupt#aGupt
Singh#aniaSingh
Roy#Roy
Gup#taGup
Gupt#aGupt

Query 3: The following SELECT query uses the SUBSTRING_INDEX function with the Student_Address column of the above Student_Marks table:

This SQL statement shows the characters from the second position to the given symbol in the state of each student.

Output:

Student_StateSUBSTRING_INDEX_@symbol
Pun@jabun
Harya@naarya
Uttar Pr@adeshttar Pr
Rajasthan@ajasthan
Punjab@unjab
Uttar Pra@deshttar Pra
Uttar Pr@adeshttar Pr





Latest Courses