MySQL LEFT() Function

In MySQL, the LEFT() function returns the leftmost N characters of a string.

If you want to get the leftmost N characters of a string, use the RIGHT() function.

LEFT() Syntax

Here is the syntax of MySQL LEFT() function:

LEFT(string, length)

Parameters

string
Required. The string from which characters need to be extracted.
length
Required. The number of characters that need to be extracted from the string.

Return value

LEFT(string, length) returns the leftmost N characters of a string.

  • If length exceeds the length of string, the function returns the string.
  • If length is zero or negative, the function returns an empty string.
  • The function will return NULL if either parameter is NULL.

LEFT() Examples

Here are some examples of MySQL LEFT() function.

SELECT
    LEFT('Hello', 1),
    LEFT('Hello', 2),
    LEFT('Hello', 3),
    LEFT('Hello', 0),
    LEFT('Hello', -1),
    LEFT('Hello', NULL),
    LEFT(NULL, NULL)\G
   LEFT('Hello', 1): H
   LEFT('Hello', 2): He
   LEFT('Hello', 3): Hel
   LEFT('Hello', 0):
  LEFT('Hello', -1):
LEFT('Hello', NULL): NULL
   LEFT(NULL, NULL): NULL