The SQL MID() function is used to get part of a string. It’s supported by MySQL, but not by MS SQL Server or Oracle. In SQL Server, Oracle databse, we can use SQL SUBSTRING or SQL SUBSTR instead.

SQL MID() Function Syntax

SELECT MID(ColumnName, Start [, Length])
FROM TableName

Note: The string start from 1 not 0, Length is optional, if it’s not provided, the MID() function returns the rest of the string.

SQL MID() Function Example

Table: Employees

EmployeeId FirstName LastName
101 Bkdfa Ydfada
102 Nbad Qdfda
103 Hifdad Wpay
104 Opca Trlfa
105 Bldfa Ghkdaies

Select first name and the first letter of the last name:

SELECT Firstname, MID(Lastname, 1, 1) AS Initial
FROM Employees

The result will look like:

FirstName Initial
Bkdfa Y
Nbad Q
Hifdad W
Opca T
Bldfa G

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>