SUBSTRING() function in MySQL
Last Updated :
22 Sep, 2020
Improve
SUBSTRING() :
function in MySQL is used to derive substring from any given string .It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.
Syntax :
Example-2 :
Deriving substring from a given string when length parameter is given.
Example-3 :
Deriving substring from a given string when starting position is -ve, i.e: starting from end.
Example-4 :
Extracting all substring from the text column in a Table.
Table : Student_Details
SUBSTRING(string, start, length) OR SUBSTRING(string FROM start FOR length)Parameters : This method accepts three-parameter as mentioned above and described below.
- string - Input String from which to extract.
- start - The starting position. If it is a positive number, this function extracts from the beginning of the string. If it is a negative number, this function extracts from the end of the string.
- length - It is optional. It identifies the number of characters to extract. If it is not given The whole string is returned from the starting position.
SELECT SUBSTRING("GeeksForGeeks", 3) AS Sub_String;Output :
Sub_String |
---|
eksForGeeks |
SELECT SUBSTRING("GeeksForGeeks", 3, 8) AS Sub_String;Output :
Sub_String |
---|
eksForGe |
SELECT SUBSTRING("GeeksForGeeks", -3 ) AS Sub_String;Output :
Sub_String |
---|
eks |
Student_Id | Student_Name |
---|---|
101 | Virat |
102 | Rohit |
103 | Rahul |
104 | Sikhar |
SELECT SUBSTRING( Student_Name, 2 ) AS Sub_String FROM Student_Details ;Output :
Sub_String |
---|
irat |
ohit |
ahul |
ikhar |