How Can I use Substring function in MySQL data

Mysql database has many built-in functions. They are available for completely different purposes, one of which is the Substring function. This function actively works with the “row” data type and is used in database management to retrieve rows from tables. What is this line? A string is an aggregate combination of characters. For example, “Hello_World_from here” and “test345” are strings.

Substring is a special function that is used to return or extract a string from strings in any table. The general syntax looks like this:

SUBSTRING(string, start, length)

In order for you to understand this function, you can take a table as an example and use the following commands to extract rows from it. We will open a terminal and type “mysql” to enter the MySQL environment.

sudo mysql

Next, you should open the list of databases, all that you currently have in MySQL


Next, you should open a database, for example with the name “sample database”:


In the output, we can see that the company database contains only one table named “Hello_World_from_here“. We will now access this table to display its contents:

DESCRIBE Hello_World_from_here

We can now display the contents of the table:

SELECT * FROM Hello_World_from_here

We say that we want to extract the string “Hello” 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” to return a value in the form of a string from the table …

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

This command is used as a terminal. This is necessary in order to fetch a row from a column named employee_name, start at the first character, and extract the next 5 characters. To understand this better, let’s say we want to extract 2 characters from the column named World_from_here, but it starts at the third character, so the command will be.

SELECT SUBSTRING(World_from_here, 1, 5) AS ExtractString FROM Hello_World_from_here;\

This output will deal with extracting only “dr” and “xi”. In addition, a command will be run to retrieve the string.

SELECT SUBSTRING(“Hello”, +3, 2) AS World_from_here;

So, what happened in the latest changes.

The terminal was requested to use a special MySQL function that returns string characters from the string “World” and starts at its third position from the start point, the + ve sign tells it to start at the start point and extract the next two characters. So, in the line “World”, if we start at the third position from the starting point, it will start with “d”, and if we extract two characters next to it, then it will be “dr”

Now, at the moment, you need to run the following command. This will now allow you to extract “ax” from the string “Maxim” using a starting point at the end of the string.

SELECT SUBSTRING(“Maxim”, -4, 2) AS World_from_here;

Was this article helpful?

Related Articles

Leave A Comment?