String SUBSTRING() function
Last Updated : 24 Jan 2026
The substring() is a String function of MySQL. This function returns the substring from the given string.
Syntax
In the above syntax, the substring function returns the substring of the string from the 'pos' mentioned.
In the above syntax, substring function returns the substring. In this, 'pos' indicates from where you want to extract the substring.
In the above syntax, substring function returns the substring. In this, ' 'pos' is from where you want to extract and 'len' indicates the number of characters it needs to be extracted.
In the above syntax, the substring function returns the substring. In this, 'from' indicates the keyword and 'pos' is from where you want to extract and 'for' keyword indicates for which length and 'len' indicates how many characters it needs to be extracted.
Parameter:
Str: Main string
Len: length for substring
Pos: position the start the substring from the given string.
Example 1
Explanation: On execution of the above statement, the substring function is used to find the substring of the string from position 7.
Output: The output of this example is given below.
| Result |
| tech |
Example 2
Explanation: On execution of the above statement, the substring function is used to extract a substring starting at position 7 and extracting 4 characters.
Output: The output of this example is given below.
| Result |
| ttech |
Example 3
Explanation: On execution of the above statement, the substring function is used to extract a substring starting at position 7 and extracting 4 characters.
Output: The output of this example is given below.
| Result |
| tech |
Example 4
Explanation: On execution of the above statement, the substring function is used to returns the 4 characters from the 1th position of the tpointtech string.
Output: The output of this example is given below.
| Result |
| tpoin |
Example 5
Explanation: On execution of the above statement, the substring function is used to return a negative value as the condition. In this, the resulting substring will start after the specified number of characters from the end of the given string.
Output: The output of this example is given below.
| Result |
| ch |
Using the MySQL SUBSTRING() function in Table column
We can use SUBSTRING() function in table to find the substring in a table. Follow the steps below to perform the MySQL SUBSTRING() functionality in a column of the table.
Step 1: First, create a table named EMP_INFO using the CREATE TABLE statement.
Step 2: After that INSERT data into a table using the INSERT TABLE statement.
Step 3: To check the information with the use of SELECT statement, whether the data is inserted or not.
| ID | FName | LName | Department | Salary | Phone |
|---|---|---|---|---|---|
| 1 | Anjana | Gupta | Purchase | 30000 | 9878453481 |
| 2 | Priyanka | Kapoor | Sales | 40000 | 8673193472 |
| 3 | Bobby | Kapoor | Marketing | 34000 | 8154538525 |
| 4 | Alisha | Sharma | Finance | 35000 | 9378753481 |
| 5 | Eshant | Sharma | HR | 40000 | 9878253481 |
Example:
Explanation: On execution of the above SELECT statement, the substring function is used to find the substring in the Phone column in the EMP_INFO table.
Output: The output of this example is given below.
| ID | FName | LName | Department | Result | Phone |
|---|---|---|---|---|---|
| 1 | Anjana | Gupta | Purchase | 30 | 9878453481 |
| 2 | Priyanka | Kapoor | Sales | 40 | 8673193472 |
| 3 | Bobby | Kapoor | Marketing | 34 | 8154538525 |
| 4 | Alisha | Sharma | Finance | 35 | 9378753481 |
| 5 | Eshant | Sharma | HR | 40 | 9878253481 |
Using a MySQL SUBSTRING() function with WHERE clause
With the combination of MySQL SUBSTRING() function with WHERE clause to find the restricted data. Use the following table to implement MySQL SUBSTRING() function with WHERE clause.
Sample Table: EMP_INFO
| ID | FName | LName | Department | Salary | Phone |
|---|---|---|---|---|---|
| 1 | Anjana | Gupta | Purchase | 30000 | 9878453481 |
| 2 | Priyanka | Kapoor | Sales | 40000 | 8673193472 |
| 3 | Bobby | Kapoor | Marketing | 34000 | 8154538525 |
| 4 | Alisha | Sharma | Finance | 35000 | 9378753481 |
| 5 | Eshant | Sharma | HR | 40000 | 9878253481 |
Example:
Explanation: On execution of the above SELECT statement, the substring function is used to find the substring in the Phone column in the EMP_INFO table where the salary is greater than 30000.
Output: The output of this example is given below.
| ID | FName | LName | Department | Result | Phone |
|---|---|---|---|---|---|
| 2 | Priyanka | Kapoor | Sales | 40 | 8673193472 |
| 3 | Bobby | Kapoor | Marketing | 34 | 8154538525 |
| 4 | Alisha | Sharma | Finance | 35 | 9378753481 |
| 5 | Eshant | Sharma | HR | 40 | 9878253481 |
MySQL SUBSTRING() function with the UPDATE Statement
In MySQL, the SUBSTRING() function is used to find the substring in data. With the use of UPDATE statement we can update the trimmed data in a table.
Sample Table: EMP_INFO
| ID | FName | LName | Department | Salary | Phone |
|---|---|---|---|---|---|
| 1 | Anjana | Gupta | Purchase | 30000 | 9878453481 |
| 2 | Priyanka | Kapoor | Sales | 40000 | 8673193472 |
| 3 | Bobby | Kapoor | Marketing | 34000 | 8154538525 |
| 4 | Alisha | Sharma | Finance | 35000 | 9378753481 |
| 5 | Eshant | Sharma | HR | 40000 | 9878253481 |
Use the following syntax of UPDATE statement with the SUBSTRING() to find the substring in a table.
Syntax:
Example: Consider a MySQL query using the SUBSTRING() function to find the substring in the Salary column in EMP_INFOtable.
Explanation: After executing the above update query, the SUBSTRING function is used to find the substring in the table.
If you want to check whether the content has been updated in the table or not, use the following statement.
Output: After running this query, the output of the table is given below.
| ID | FName | LName | Department | Result | Phone |
|---|---|---|---|---|---|
| 1 | Anjana | Gupta | Purchase | 30 | 9878453481 |
| 2 | Priyanka | Kapoor | Sales | 40 | 8673193472 |
| 3 | Bobby | Kapoor | Marketing | 34 | 8154538525 |
| 4 | Alisha | Sharma | Finance | 35 | 9378753481 |
| 5 | Eshant | Sharma | HR | 40 | 9878253481 |
Frequently Asked Questions on the MySQL SUBSTRING() Function
1. What are the different functions of SUBSTRING() function?
Answer: The different functions of SUBSTRING() function are given below:
- It is used to extract a specific number of characters from a particular position in a given string, by specifying their length and position in the string.
- It is mostly used in data manipulation tasks, such as extracting specific parts of a string for further processing or modifying the contents of a string based on a desired substring.
2. Write a MySQL query to update records in a table so that only the first letter is capitalized and all other letters are set to lowercase.
| TID | FName | LName | Department | Salary | Phone |
|---|---|---|---|---|---|
| 1 | Anjana | Gupta | Purchase | 30000 | 9878453481 |
| 2 | Priyanka | Kapoor | Sales | 40000 | 8673193472 |
| 3 | Bobby | Kapoor | Marketing | 34000 | 8154538525 |
| 4 | Alisha | Sharma | Finance | 35000 | 9378753481 |
| 5 | Eshant | Sharma | HR | 40000 | 9878253481 |
Example:
If you want to check whether the content has been updated in the table or not, use the following statement.
Output: After running this above query, the output of the table is given below.
| TID | Name |
|---|---|
| 1 | Anjana |
| 2 | Priyanka |
| 3 | Bobby |
| 4 | Alisha |
| 5 | Eshant |
3. Is it possible to use SUBSTRING() in a WHERE clause?
Answer: Yes, SUBSTRING() can be used in a WHERE clause to filter records based on substring values, such as matching or extracting parts of column data for conditional logic.
Next TopicMySQL String