The LEFT() function in MySQL returns a specific number of characters from the start (left side) of a string.
MySQL LEFT Function Syntax
SQL
x
1
LEFT(string, number_of_chars)
MySQL LEFT Function Example
This query extracts the first 3 characters from the string “SQL Tutorial”:
SQL
1
1
SELECT LEFT("SQL Tutorial", 3) AS ExtractString;
Note: This example works in MySQL, but does not work in SQLite or the TechKubo Playground.
MySQL LEFT Function Parameters
Parameter | Description |
---|---|
string | Required. The string to extract from |
number_of_chars | Required. Number of characters to extract from left |
MySQL LEFT Function Patients Table Example
This query attempts to extract the first 2 characters from each first_name in the Patients
table:
SQL
1
1
SELECT LEFT(first_name, 2) AS Initials
2
FROM Patients;
Note: This example works in MySQL, but does not work in SQLite or the TechKubo Playground.
MySQL LEFT Function Notes
- If the number of characters is greater than the string length, the whole string is returned.
- In SQLite (including TechKubo), use
SUBSTR(column, 1, N)
instead ofLEFT()
.- Available in MySQL 4.0 and later.