In this article, we will understand what is the substring function and how it can be used in MySQL in managing the data in the databases.
What is MySQL substring
MySQL has a variety of functions that perform different tasks, similarly, Substring is a function that is used in MySQL to return or to extract the string from strings in any table. The general syntax is as:
If we explain the parameters used in these general syntaxes then they will be as; string is the collection of the characters, start means the starting point from where it is supposed to extract the string and length is the number of characters that are to be extracted. To understand the function of the substring, we take an example of the table and use the following commands to extract strings from it. We will open the terminal, and type “mysql” to enter the MySQL environment.
Open the list of databases, present in MySQL:
Open the database, “company”:
Now, I will show the tables present in this database as.
In the output, we can see the database, company, contains only one table which is named “linuxHint_employees_data”. Now we will access this table to display its contents:
Now we can display the contents of the table:
We say that we want to extract the string “John”, from the table using the substring function then we will use the code according to the syntax we discussed above and we will use “AS ExtractString ” so it returns the value in the form of string from the table.
In this command, we use the terminal to extract a string from the column named employee_name, start from the first character, and extract the next 5 characters. To understand it more, let us say we want to extract the 2 characters from the column, named employee_name but it starts from the third character so the command will be.
As we know it will extract only “hn” and “ew”.
Also, we run the following command to extract the string.
Let us explain what happened in the above command and how this output came. We ask the terminal using this function of MySQL that returns the string characters from the string “John” and starts from its third position from the starting point, +ve sign tells it to start from the starting point and extract the next two characters. So in the string “John”, if we start from the third position from the starting point it will start from “h” and if we extract two characters next to it then it will be “hn”. This is shown in the output.
Now let us run another command to extract “wa” from the string “Steward” using the starting point from the end of the string so for it we will use the -ve sign and the command will be.
Substring is a function of MySQL which is used to extract either the whole string or a few characters of the string. In this write-up, we tried to explain to you what a substring function is and for which purpose it is used. We also discussed two general syntaxes and then explained the use of both these syntaxes in detail with the help of examples. Hope this write-up will help you not in understanding the function substring but how it can be used.