SUBSTRING Function in SQL

The SUBSTRING string function in Structured Query Language shows the characters or sub-string from the specific index value of the original string. We also use the SUBSTRING function with the SQL tables.

Syntax of SUBSTRING String Function

Syntax 1: This syntax uses the SUBSTRING function with the column name of the SQL table:

In the syntax of substring function, we have to define the name of that column on which we want to execute it. Here, the Length_of_string parameter is optional. If it is omitted, then this function shows the whole string from the starting index value.

Syntax 2: This syntax uses the SUBSTRING function with the string:

Syntax 3: This syntax uses the SUBSTRING function with a single character:

Examples of SUBSTRING String function

Example 1: The following SELECT query shows the characters from the 17th position of the given string.

This SQL query returns the 25 characters with spaces after the 17th position in the string.

Output:

substring_17_25
Website for professionals

Example 2: The following SELECT query shows the characters from the -16th position of the given string:

This SQL query shows the five characters from the last 16th position of the string.

Output:

substring_-16_5
or pr

Example 3: The following SELECT query shows all the characters from the 5th position of the string.

Output:

substring_5
Delhi IS the Capital OF India

Example 4: The following SELECT query shows the single character from the 6th position of the string.

Output:

substring_6_1
o

Example 5: This example uses the SUBSTRING function with the SQL table

In this example, we are going to create a new SQL table on which we want to execute the SUBSTRING function.

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_NoFirst_NameLast_NameFirst_CitySecond_CityNew_CityHindi_MarksMaths_MarksGrade
10AmanSharmaLucknowChandigarhGhaziabad8895A2
02VishalSharmaChandigarhGhaziabadGhaziabad9582A1
07RajGuptaDelhiGhaziabadLucknow9195A1
04YashSinghaniaGhaziabadDelhiLucknow8582A2
11VinayRoyDelhiKanpurGhaziabad9597A1
16ManojGuptaGhaziabadMeerutChandigarh9590B1
19RamGuptaLucknowGhaziabadChandigarh8995A2

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

This SQL statement shows the four characters from the 2nd position of the Last name of each student.

Output:

Last_NameSUBSTRING_2_4
Sharmaharm
Sharmaharm
Guptaupta
Singhaniaingh
Royoy
Guptaupta
Guptaupta

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

This SQL statement shows the two characters from the third last position of the Last name of each student.

Output:

Last_NameSUBSTRING_-3_2
Sharmarm
Sharmarm
Guptapt
Singhaniani
RoyRo
Guptapt
Guptapt





Latest Courses