w3resource

MySQL LEFT() function

LEFT() function

MySQL LEFT() returns a specified number of characters from the left of the string. Both the number and the string are supplied as arguments of the function.

This function is useful in -

  • Substring extraction: It extracts characters from the left side of a string.
  • Data truncation: The LEFT() function truncates or limits the length of a string by extracting only a certain number of characters.
  • Data manipulation: LEFT() is commonly used to extract part of a string or modify its content based on the leftmost characters in a string.

Syntax:

LEFT (string, length)

Arguments:

Name Description
string The string from which a number of characters from the left are to be returned.
length An integer which indicates the number of characters to be returned starting from the left of the string in the first argument.

Syntax Diagram:

MySQL LEFT() Function - Syntax Diagram

MySQL Version: 8.0

Pictorial Presentation:

MySQL LEFT() pictorial presentation

Example of MySQL LEFT() function

The following MySQL statement returns the pub_name and 5 characters from the left of pub_name from the publisher table.

Code:

SELECT pub_name, LEFT(pub_name, 5) 
FROM publisher; 

Sample table: publisher


Output:

mysql> SELECT pub_name, LEFT(pub_name, 5) 
    -> FROM publisher;
+------------------------------+-------------------+
| pub_name                     | LEFT(pub_name, 5) |
+------------------------------+-------------------+
| Jex Max Publication          | Jex M             | 
| BPP Publication              | BPP P             | 
| New Harrold Publication      | New H             | 
| Ultra Press Inc.             | Ultra             | 
| Mountain Publication         | Mount             | 
| Summer Night Publication     | Summe             | 
| Pieterson Grp. of Publishers | Piete             | 
| Novel Publisher Ltd.         | Novel             | 
+------------------------------+-------------------+
8 rows in set (0.00 sec)

Example of MySQL LEFT() function with where clause

The following MySQL statement returns the pub_name(s) who belong to the USA and 5 characters from the left of those pub_name(s) from the publisher table.

Code:

SELECT pub_name,LEFT(pub_name,5) 
FROM publisher 
WHERE country='USA'; 

Sample table: publisher


Output:

mysql> SELECT pub_name,LEFT(pub_name,5) 
    -> FROM publisher 
    -> WHERE country='USA';
+--------------------------+------------------+
| pub_name                 | LEFT(pub_name,5) |
+--------------------------+------------------+
| Jex Max Publication      | Jex M            | 
| Mountain Publication     | Mount            | 
| Summer Night Publication | Summe            | 
+--------------------------+------------------+
3 rows in set (0.00 sec)

Video Presentation:

All String Functions (Slides presentation)

Previous: LCASE
Next: LENGTH



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/string-functions/mysql-left-function.php