SQL MID() Function

MID() Function

The MID function is used to extract characters from text fields.

SQL MID() Syntax

SELECT MID(column_name,start[,length]) FROM table_name
Parameters Description
column_name Required. The field to extract characters from.
start Required. Specifies the starting position (the starting value is 1).
length Optional. The number of characters to return. If omitted, the MID() function returns the remaining text.

SQL MID() Example

We have the following "Persons" table:

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

Now, we want to extract the first 3 characters from the "City" column.

We use the following SQL statement:

SELECT MID(City,1,3) as SmallCity FROM Persons

The result set is similar to this:

SmallCity
Lon
New
Bei