WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL MID() Function


MID () function

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

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 (starting value is 1).
length Optional. The number of characters to return. If omitted, the MID () function returns the remaining text.


Demo database

In this tutorial, we will use the welookups sample database.

Here is the data from the "Websites" table:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/  | 1     | USA     |
| 2  | zomato          | https://www.zomato.com/ | 13    | IN      |
| 3  | indiamart      | http://www.indiamart.com/  | 4689  | IN      |
| 4  | Yahoo          | http://Yahoo.com/       | 20    | IN      |
| 5  | Facebook     | https://www.facebook.com/| 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/|   0 | IND     |
+----+---------------+---------------------------+-------+---------+


SQL MID () instance

The following SQL statement extracts the first 4 characters from the "name" column of the "Websites" table:

Example

SELECT MID(name,1,4) AS ShortTitle
FROM Websites;