MariaDB LEFT() Function

In MariaDB, the LEFT() function extracts a given number of characters from the left of a string and returns them as a string.

If you want to extract characters from the right of a string, use the RIGHT() function.

MariaDB LEFT() Syntax

Here is the syntax of the MariaDB LEFT() function:

LEFT(str, len)

Parameters

str

Required. The string from which characters need to be extracted.

len

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

If you provide no parameters or use the wrong number of parameters, MariaDB will report an error: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1.

Return value

MariaDB LEFT(str, len) extracts the specified number of characters from the left of the specified string and returns them as a string.

If len exceeds the length of str, the LEFT() function returns str.

If len is zero or negative, the LEFT() function returns an empty string.

If any of the arguments is NULL, the LEFT() function will return NULL.

MariaDB LEFT() Examples

To extract 2 characters from the left of ABCD, use the following statement:

SELECT LEFT('ABCD', 2);

Output:

+-----------------+
| LEFT('ABCD', 2) |
+-----------------+
| AB              |
+-----------------+

A few common examples for LEFT().

SELECT
    LEFT('Hello', 1),
    LEFT('Hello', 2),
    LEFT('Hello', 3),
    LEFT('Hello', 0),
    LEFT('Hello', -1),
    LEFT('Hello', NULL),
    LEFT(NULL, NULL)\G

Output:

   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

Conclusion

The MariaDB LEFT() function extracts the specified number of characters from the left of a string.