This MySQL function returns the first length characters from a string.


Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

LEFT(string, length)


This function returns the first length characters from a string. If you want to extract the end of the string instead of the beginning, use the RIGHT( ) function. Both are multi-byte safe.


SELECT LEFT(phone_home, 3) AS 'Area Code',
FROM students
GROUP BY LEFT(phone_home, 3);

Using the LEFT() function, this statement extracts the first three digits of phone_home for each row, which is the telephone area code (i.e., city code). It then groups the results, using the same function in the WHERE clause. This returns a count of the number of students living in each telephone area code.