MySQL MID Function

The MySQL MID function is a synonym of the Substring method. This MID Function returns or extracts a substring from a given string. Let me show you how to write a String MID with an example.

MySQL MID Function Syntax

The basic syntax of the MID string is as shown below.

MID(String, Start_Position, Length)

MID(String, Start_Position)

Here, Length = number of characters to extract or return. The MID function allows you to use negative values for the start Position argument. If you use negative values, then it looks from right to left (end to start position).

MySQL MID Function Example

The following query shows multiple ways to use this Mid string function. First, We used to start at index position 6. The second statement, it starts at index position 13.

Within the third statement, we used the length (third argument) as 20. It means it starts at index position 6 and returns 20 characters from position 6.

SELECT MID('abc is working in xyz Company', 6);

SELECT MID('abc is working in xyz Company', 13);

SELECT MID('abc is working in xyz Company', 6, 20);
Middle String Example 1

MID Example 2

In this String method example, we used negative values as the index position argument. The first statement starts at index position -12. It means the MySQL mid string function begins at the 12th index from the right side (i.e.,r).

SELECT MID('Suresh is working for Tutorial Gateway', -12);

SELECT MID('Suresh is working for Tutorial Gateway', -25);

SELECT MID('Suresh is working for Tutorial Gateway', -30);
Middle Text Example 2

Here, we used the Start position as negative values and length argument. The first MySQL statement starts at the 12th position from the right side of a string and returns four characters (length = 4).

SELECT MID('Suresh is working for Tutorial Gateway', -12, 4);

SELECT MID('Suresh is working for Tutorial Gateway', -25, 15);

SELECT MID('Suresh is working for Tutorial Gateway', 5, 22);
Example 3

MID Example 3

The String MID function also allows you to select or extract the required number of characters from the column values.

In this example, We are going to use this string Mid function with different start positions and lengths on the Email column. Please refer to the Substring method article.

SELECT FirstName,
       LastName,
       DepartmentName,
       Email,
       MID(Email, 3),
       MID(Email, -9),
       MID(Email, 3, 6),
       MID(Email, -10, 5)
 FROM employe;
MySQL MID Function 4