MySQL LEFT Function: Truncate A String From The Left

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:

IDEmail
1john.doe@gmail.com
2jane.doe@yahoo.com
3bob.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:

IDEmailUsername
1john.doe@gmail.comjohn.doe
2jane.doe@yahoo.comjane.doe
3bob.smith@aol.combob.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.

  1. MySQL SUBSTRING function example:

    • Description: The SUBSTRING function is used to extract a substring from a string in MySQL.
    • Example:
      SELECT SUBSTRING('Hello, World!', 1, 5) AS Result;
      -- Output: Hello
      
  2. How to truncate a string from the left in MySQL:

    • Description: Use SUBSTRING to truncate a string from the left.
    • Example:
      SELECT SUBSTRING('Hello, World!', 8) AS Result;
      -- Output: World!
      
  3. LEFT vs SUBSTRING in MySQL:

    • Description: LEFT and SUBSTRING are similar, but LEFT is often used for simplicity when truncating from the left.
    • Example using LEFT:
      SELECT LEFT('Hello, World!', 5) AS Result;
      -- Output: Hello
      
  4. MySQL RIGHT function for truncating:

    • Description: Use RIGHT to truncate a string from the right.
    • Example:
      SELECT RIGHT('Hello, World!', 6) AS Result;
      -- Output: World!
      
  5. Truncate string from the left using MySQL functions:

    • Description: Combine LENGTH and SUBSTRING to truncate from the left.
    • Example:
      SELECT SUBSTRING('Hello, World!', LENGTH('Hello, World!') - 5 + 1) AS Result;
      -- Output: World!
      
  6. Substring left side of a string in MySQL:

    • Description: Use SUBSTRING to extract the left side of a string.
    • Example:
      SELECT SUBSTRING('Hello, World!', 1, 7) AS Result;
      -- Output: Hello, 
      
  7. MySQL truncate string length from the left:

    • Description: Truncate a string from the left using the length parameter of SUBSTRING.
    • Example:
      SELECT SUBSTRING('Hello, World!', 1, LENGTH('Hello, World!') - 6) AS Result;
      -- Output: Hello
      
  8. Using SUBSTRING to cut characters from the left in MySQL:

    • Description: Demonstrate how to cut characters from the left using SUBSTRING.
    • Example:
      SELECT SUBSTRING('MySQL is powerful', 7) AS Result;
      -- Output: is powerful
      
  9. Examples of truncating strings in MySQL queries:

    • Description: Various examples demonstrating different ways to truncate strings in MySQL queries.
    • Examples:
      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;
      
  10. MySQL string manipulation functions for truncation:

    • Description: Overview of MySQL string manipulation functions like SUBSTRING and LEFT for truncation.
    • Examples:
      SELECT SUBSTRING('MySQL is powerful', 7) AS Result1,
             LEFT('Another example', 8) AS Result2;