Loading

SQL MID() Function

Submitted by: 
Language: 
Visitors have accessed this post 9686 times.


The MID() function is used to extract values from a column. The MID() contain three (3) parameters. The first one is used to select which column to extract, the second one is the starting position, the last one is the number of characters to be extracted.

SQL MID() Syntax

SELECT MID(column_name,START[,LENGTH]) FROM TABLE_NAME

Consider the following table for this exercise

Users

Firstname Lastname Salary DeptID
John Smith 1000 1
Mathew Simon 3000 1
Bill Steve 2200 1
Amanda Rogers 1800 2
Steve Hills 2800 2
Steve jobs 2400 2
bill cosby 700 3

Example # 1

SELECT MID(Firstname,1,3) FROM Users

This will extract the Firstname column from our table.

Result of the Query

Firstname
JOH
MAT
BIL
AMA
STE
STE
BIL

The result shows the first three (3) characters of the Firstname column.

You can also start the position to whatever you like.

Example # 2

SELECT MID(Firstname,2,3) FROM Users

This will extract the value of Firstname starting from 2.

Firstname
ohn
ath
ill
man
tev
tev
ill



Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.