Name

MID( )

Synopsis

MID(string, position[, length])

This function returns the characters of a given string, starting from the left position specified in the second argument. The first character is numbered 1. You can limit the length of the string retrieved by specifying a limit in the third argument. This function is similar to SUBSTRING( ).

SELECT CONCAT(name_first, ' ', name_last) AS Teacher,
       CONCAT('(', LEFT(home_telephone, 3), ') ',
       MID(home_telephone, 4, 3), '-',
       MID(home_telephone, 7)) AS Telephone
FROM teachers LIMIT 1;
+----------------+----------------+
| Teacher        | Telephone      |
+----------------+----------------+
| Olympia Vernon | (504) 230-1748 |
+----------------+----------------+

This convoluted SQL statement produces the output shown by concatenating the extracted components of the selected teacher’s telephone number with opening and closing parentheses, a dash, and a space.

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.