MySQL8 MID() Functions – String Functions
MID(), This function returns a substring starting from the specified position.
Both MID() and SUBSTR() are synonyms of SUBSTRING().
The forms without a length argument return a substring from a string starting at a specified position. See illustrations 1 and 2.
The forms with a length argument return a substring length characters long from the string, starting at a specified position. See illustrations 3 and 5.
The forms that use FROM are standard SQL syntax.
It is also possible to use a negative(-ve) value for the position. In this case, the beginning of the substring is position characters from the end of the string, rather than the beginning.
A negative(-ve) value may be used for the position in any of the forms of this function. See illustrations 6 and with every illustration, you can see how negative the position value will work.
A value of 0 for position returns an empty string. See illustrations 7.
If length < 1, the result is the empty string. See illustrations 8.
For all forms of MID(), the position of the first character in the string from which the substring is to be extracted is to take into account as 1.
MID() : Syntax
Below are several ways on how we can use this function, so the full range of syntax’s looks like this:
MID(string , position );
MID(string FROM position );
MID (string , position, length);
MID(string FROM position FOR length);
MID() : Parameter
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
string , Required, String , The input string.
position, Required, Numeric, The position from where the substring starts.
length, Optional, Numeric , The length tells the number of characters to return from that starting position.
[/table]
MID() : Output
[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Returns,
String, returns a substring starting from the specified position.
[/table]
MID() : Available from
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 4.0
[/table]
MID() Example 1 : Basic usage with MID(string, position)
Below is an example with MID(string, position):
mysql> SELECT MID('database mysql', 5 ) as Output; +------------+ | Output | +------------+ | base mysql | +------------+ 1 row in set (0.00 sec)
With a negative value for the position
mysql> SELECT MID('database mysql', -5 ) as Output; +--------+ | Output | +--------+ | mysql | +--------+ 1 row in set (0.04 sec)
MID() Example 2 : Usage With FROM Clause
Below is an example :
mysql> SELECT MID('database mysql' FROM 5 ) as Output; +------------+ | Output | +------------+ | base mysql | +------------+ 1 row in set (0.00 sec)
With a negative value for the position
mysql> SELECT MID('database mysql' FROM -5 ) as Output; +--------+ | Output | +--------+ | mysql | +--------+ 1 row in set (0.00 sec)
MID() Example 3 : With Specify a Length
Below is an example of it.
mysql> SELECT MID('database mysql' , 5, 4 ) as Output; +--------+ | Output | +--------+ | base | +--------+ 1 row in set (0.06 sec)
With a negative value for the position
mysql> SELECT MID('database mysql' , -10, 4 ) as Output; +--------+ | Output | +--------+ | base | +--------+ 1 row in set (0.00 sec)
MID() Example 4 : NULL arguments
It will return NULL, if any of the arguments is NULL. Let’s see the below example.
SELECT MID('database mysql' ,NULL, 4 ) as Output1, MID('database mysql',5 ,NULL ) as Output2, MID(NULL, 3, 4 ) as Output3; +---------+---------+---------+ | Output1 | Output2 | Output3 | +---------+---------+---------+ | NULL | NULL | NULL | +---------+---------+---------+ 1 row in set (0.00 sec)
MID() Example 5 : Specify a Length (using the FOR Clause)
Let’s see the below example.
mysql> SELECT MID('database mysql' FROM 5 FOR 4) as Output; +--------+ | Output | +--------+ | base | +--------+ 1 row in set (0.00 sec)
With a negative value for the position
mysql> SELECT MID('database mysql' FROM -12 FOR 4) as Output; +--------+ | Output | +--------+ | taba | +--------+ 1 row in set (0.00 sec)
MID() Example 6 : Using a negative value for the position
Let’s see the below example.
mysql> SELECT MID('database mysql',-5 ); +---------------------------+ | MID('database mysql',-5 ) | +---------------------------+ | mysql | +---------------------------+ 1 row in set (0.00 sec)
MID() Example 7 : Using Zero(0) value for position variable
Let’s see the below example.
mysql> SELECT MID('database mysql' FROM 0 FOR 4); +------------------------------------+ | MID('database mysql' FROM 0 FOR 4) | +------------------------------------+ | | +------------------------------------+ 1 row in set (0.00 sec)
MID() Example 8 : Using Negative value i.e less than 1 for length variable
Let’s see the below example.
mysql> SELECT MID('database mysql', 10, -5); +-------------------------------+ | MID('database mysql', 10, -5) | +-------------------------------+ | | +-------------------------------+ 1 row in set (0.00 sec)
See all MySQL String functions MySQL 8 String Functions.