MySQL: MID Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL MID Function

In this guide, we will explain 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 MID function examples and explore how to use the MID function.

For example:

mysql> SELECT MID('adglob.in', 5, 2);
Result: 'ob'

mysql> SELECT MID('adglob.in', 1, 4);
Result: 'adgl'

mysql> SELECT MID('adglob.in', -2, 2);
Result: 'in'

mysql> SELECT MID('adglob.in', -6, 3);
Result: 'lob'

Next Topic : Click Here

This Post Has One Comment

Leave a Reply