MySQL LEFT() Function: Extract Characters from the Left of a String
Learn how to use the MySQL `LEFT()` function to efficiently extract a specified number of characters from the beginning of a string. This guide explains the function's syntax, provides practical examples, and demonstrates how to use `LEFT()` for effective text manipulation in your MySQL queries.
MySQL LEFT() Function
Definition and Usage
The LEFT()
function in MySQL extracts a specified number of characters from the beginning (left side) of a string. It's a handy function for working with text data.
Syntax
Syntax
LEFT(string, number_of_chars)
Parameter Values
Parameter | Description |
---|---|
string |
Required. The string from which you want to extract characters. |
number_of_chars |
Required. The number of characters to extract from the left. If this number is larger than the string's length, the entire string is returned. |
Examples
Example 1: Extracting Characters from a Literal String
This example extracts the first three characters from the string "SQL Tutorial".
SQL Query
SELECT LEFT("SQL Tutorial", 3) AS ExtractString;
Output
ExtractString
-------------
SQL
Example 2: Extracting Characters from a Column
This example shows how to extract the first 5 characters from the "CustomerName" column in a "Customers" table.
SQL Query
SELECT LEFT(CustomerName, 5) AS ExtractString FROM Customers;
Output
(This output will vary depending on the data in the Customers table. It will show the first 5 characters of each CustomerName.)
Technical Details
The LEFT()
function has been available in MySQL since version 4.0.
Tip
For extracting characters from the right side of a string, see the RIGHT()
function.