Mysql Mid Function

MySQL: MID Function

This tutorial explains how to use the MySQL MID function with syntax and examples.

Description

The MySQL MID function allows you to extract a substring from a string.

Syntax

The syntax for the MID function in MySQL is:

MID( string, start_position, length )

Parameters or Arguments

string

The source string used to extract from.

start_position

The position to begin extraction. The first position in the string is always 1.

length

The number of characters to extract.

Note

  • The first position in string is 1.
  • If start_position is a positive number, then the MID function starts from the beginning of the string.
  • If start_position is a negative number, then the MID function starts from the end of the string and counts backwards.
  • The MID function and the SUBSTR function are synonyms of the SUBSTRING function.

Applies To

The MID function can be used in the following versions of MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let's look at some MySQL MID function examples and explore how to use the MID function in MySQL.

For example:

mysql> SELECT MID('AODBA.com', 5, 2);
Output: 'A.'

mysql> SELECT MID('AODBA.com', 1, 4);
Output: 'AODB'

mysql> SELECT MID('AODBA.com', -3, 3);
Output: 'com'

mysql> SELECT MID('AODBA.com', -7, 3);
Output: 'DBA'