MySQL MariaDB

How to use MySQL Substring function

Mysql has a variety of built-in functions for different purposes, one of them is the function of the substring. The function of substring deals with the “string” data type and it is used in database management to extract the string from the tables. Now what the string is? The string is the collective combination of the characters. For example, “Hello LinuxHint” and “KHG1234” are strings.

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:

$ SUBSTRING(string, start, length)

Or:

$ SUBSTRING(string FROM start FOR length)

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.

$ sudo MySQL

Open the list of databases, present in MySQL:

Open the database, “company”:

USE 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:

DESCRIBE linuxHint_employees_data;

Now we can display the contents of the table:

SELECT * FROM linuxHint_employees_data;

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.

SELECT SUBSTRING(employee_name, 1, 5) AS ExtractString FROM linuxHint_employees_data;

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.

SELECT SUBSTRING(employee_name, 3, 2) AS ExtractString FROM linuxHint_employees_data;

As we know it will extract only “hn” and “ew”.

Also, we run the following command to extract the string.

SELECT SUBSTRING(“John”, +3, 2) AS ExtractString;

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.

SELECT SUBSTRING(“Steward”, -4, 2) AS ExtractString;

Conclusion

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.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.