MySQL MID Function

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

MySQL MID Function Syntax

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

MID(String, Start_Position, Length)

MID(String, Start_Position)

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

MySQL MID Function Example 1

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

Within the third statement, we used the length (third argument) as 20. It means, Mid function 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);
MySQL MID Function 1

MySQL MID Example 2

In this String Function example, we used negative values as the index position argument. The first statement starts at index position -12. It means MySQL Mid function starts at the 12th index position 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);
MySQL MID Function 2

Here, we used the Start position as negative values and length argument. The first MySQL statement starts at 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);
MySQL MID Function 3

MySQL String MID Example 3

The MySQL 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 MySQL string Mid function with different start positions, and lengths on the Email column. Please refer to MySQL Substring function article.

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