THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL MID() Function


The MID() Function

The MID() function is used to extract characters from a text field.

SQL MID() Syntax

SELECT MID(column_name,start,length) AS some_name FROM table_name;
Parameter Description
column_name Required. The field to extract characters from
start Required. Specifies the starting position (starts at 1)
length Optional. The number of characters to return. If omitted, the MID() function returns the rest of the text

Note: The equivalent function for SQL Server is SUBSTRING():

SELECT SUBSTRING(column_name,start,length) AS some_name FROM table_name;

Database

In this tutorial we will use the well-known database.

Below is a selection from the "Customers" table:

CustomerID CustomerName Phone Address City Country
1

Ramonita Homan 231-596-2048 ORubaiyat Road Grand Rapid MI USA
2 Charles L. Emmons 507-256-8113 Rosewood Court Clarks Grove MN USA
3 Ruth C. Dorsey Quay Street NAILBRIDGE YO4 5SS UK/td>
4

Evelyn B. Blacker 077 6011 6540 Wade Lane SAND HOLE YO4 5SS UK
5 Nytorpsv채gen 45 Christina Berglund Nytorpsv채gen 45 BANKERYD Sweden

SQL MID() Example

The following SQL statement selects the first four characters from the "City" column from the "Customers" table:

Example

SELECT MID(City,1,4) AS ShortCity
FROM Customers;