SUBSTRING() function in MySQL
Last Updated :
22 Sep, 2020
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 :
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.
Example-1 :
Deriving substring from a given string without giving length parameter.
SELECT SUBSTRING("GeeksForGeeks", 3) AS Sub_String;
Output :
Example-2 :
Deriving substring from a given string when length parameter is given.
SELECT SUBSTRING("GeeksForGeeks", 3, 8) AS Sub_String;
Output :
Example-3 :
Deriving substring from a given string when starting position is -ve, i.e: starting from end.
SELECT SUBSTRING("GeeksForGeeks", -3 ) AS Sub_String;
Output :
Example-4 :
Extracting all substring from the text column in a Table.
Table : Student_Details
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 |
Share your thoughts in the comments
Please Login to comment...