Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

MID

Example

Retrieve a substring from a string starting at position 5 and extracting 3 characters

SELECT MID(“SQL Tutorial”, 5, 3) AS ExtractString; 

 

Definition and Usage

The MID() function extracts a substring from a string starting at any position.

Note: The first character in the string is at position 1.

1 2 3 4 5 6 7 8 9 10 11 12

S Q L T u t o r i a l

 The last character in the string is at position -1.

12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

S Q L T u t o r i a l

Note: The MID() and SUBSTR() functions are equivalent to the SUBSTRING() function.

Syntax

MID(string, start, length)

Parameter Values

Parameter

Description

string

Input required: The string from which the substring will be extracted.

start

Input required: The start position, which can be either positive or negative. If positive, the function extracts from the beginning of the string. If negative, it extracts from the end of the string.

length

Input required: The number of characters to extract from the string.

Technical Details

Works in: From MySQL 4.0

More Examples

Example

Retrieve a substring from the text in a column starting at position 2 and extracting 5 characters.

SELECT MID(CustomerName, 2, 5) AS ExtractString
FROM Customers; 

Example

Retrieve a substring from a string starting from the end, at position -5, and extract 5 characters.

SELECT MID(“SQL Tutorial”, 5, 5) AS ExtractString;