The LEFT() function in MySQL is used to extract a specified number of characters from the left side of a string. This function can be useful when you need to parse the data in a column that contains text.
Syntax
Here's the basic syntax of the LEFT() function:
LEFT(string, number_of_chars)
string: This is the original string from which you want to extract characters.number_of_chars: This is the number of characters you want to extract from the left side of the string.Examples
Let's look at a few examples of how to use the LEFT() function.
Example 1:
SELECT LEFT('Hello World', 5); Output:
'Hello'
In this example, we're extracting the first 5 characters from the string 'Hello World'.
Example 2:
Suppose we have a table named employees with the following data:
| ID | |
|---|---|
| 1 | john.doe@gmail.com |
| 2 | jane.doe@yahoo.com |
| 3 | bob.smith@aol.com |
And we want to extract the first part of each email address (before the '@' character). We can use the LEFT() function in combination with the LOCATE() function:
SELECT ID, Email, LEFT(Email, LOCATE('@', Email) - 1) as Username FROM employees; Output:
| ID | Username | |
|---|---|---|
| 1 | john.doe@gmail.com | john.doe |
| 2 | jane.doe@yahoo.com | jane.doe |
| 3 | bob.smith@aol.com | bob.smith |
In this example, the LOCATE() function is used to find the position of the '@' character in the email address, and then we subtract 1 to get the position of the last character of the username. The LEFT() function then extracts that many characters from the left of the email address.
Note: It's important to note that if the number_of_chars parameter is greater than the length of the string, the LEFT() function will return the whole string. If number_of_chars is negative, the LEFT() function will return an empty string.
MySQL SUBSTRING function example:
SUBSTRING function is used to extract a substring from a string in MySQL.SELECT SUBSTRING('Hello, World!', 1, 5) AS Result; -- Output: Hello How to truncate a string from the left in MySQL:
SUBSTRING to truncate a string from the left.SELECT SUBSTRING('Hello, World!', 8) AS Result; -- Output: World! LEFT vs SUBSTRING in MySQL:
LEFT and SUBSTRING are similar, but LEFT is often used for simplicity when truncating from the left.LEFT:SELECT LEFT('Hello, World!', 5) AS Result; -- Output: Hello MySQL RIGHT function for truncating:
RIGHT to truncate a string from the right.SELECT RIGHT('Hello, World!', 6) AS Result; -- Output: World! Truncate string from the left using MySQL functions:
LENGTH and SUBSTRING to truncate from the left.SELECT SUBSTRING('Hello, World!', LENGTH('Hello, World!') - 5 + 1) AS Result; -- Output: World! Substring left side of a string in MySQL:
SUBSTRING to extract the left side of a string.SELECT SUBSTRING('Hello, World!', 1, 7) AS Result; -- Output: Hello, MySQL truncate string length from the left:
SUBSTRING.SELECT SUBSTRING('Hello, World!', 1, LENGTH('Hello, World!') - 6) AS Result; -- Output: Hello Using SUBSTRING to cut characters from the left in MySQL:
SUBSTRING.SELECT SUBSTRING('MySQL is powerful', 7) AS Result; -- Output: is powerful Examples of truncating strings in MySQL queries:
SELECT SUBSTRING('Example 1: Truncate from left', 12) AS Result1, SUBSTRING('Example 2: Truncate from right', 1, LENGTH('Example 2: Truncate from right') - 6) AS Result2; MySQL string manipulation functions for truncation:
SUBSTRING and LEFT for truncation.SELECT SUBSTRING('MySQL is powerful', 7) AS Result1, LEFT('Another example', 8) AS Result2; javascript-intellisense cqlsh mobile-website eof view getattribute resteasy tmp caliburn.micro classloader