Javatpoint Logo
Javatpoint Logo

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_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 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_Name SUBSTRING_2_4
Sharma harm
Sharma harm
Gupta upta
Singhania ingh
Roy oy
Gupta upta
Gupta upta

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_Name SUBSTRING_-3_2
Sharma rm
Sharma rm
Gupta pt
Singhania ni
Roy Ro
Gupta pt
Gupta pt






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