MariaDB Tutorial MariaDB Advanced MariaDB Database Account Management MariaDB References

MariaDB LEFT() Function



The MariaDB LEFT() function is used to extract a substring from a string, starting from the left-most character.

Syntax

LEFT(string, number_of_chars)

Parameters

string Required. Specify the string to extract from.
number_of_chars Required. Specify the number of characters to extract. If this parameter exceeds the length of the string, this function will return string.

Return Value

Returns the substring extracted from specified string.

Example 1:

The example below shows the usage of LEFT() function.

SELECT LEFT('AlphaCodingSkills.com', 1);
Result: 'A'

SELECT LEFT('AlphaCodingSkills.com', 5);
Result: 'Alpha'

SELECT LEFT('AlphaCodingSkills.com', 21);
Result: 'AlphaCodingSkills.com'

SELECT LEFT('AlphaCodingSkills.com', 50);
Result: 'AlphaCodingSkills.com'

SELECT LEFT('Alpha Coding Skills', 5);
Result: 'Alpha'

Example 2:

Consider a database table called Employee with the following records:

PhoneNumberEmpIDAddress
+33-14XXXXX011Grenelle, Paris, France
+31-20XXXXX192Geuzenveld, Amsterdam, Netherlands
+86-10XXXXX4583Yizhuangzhen, Beijing, China
+65-67XXXXX44Yishun, Singapore
+81-35XXXXX725Koto City, Tokyo, Japan

In the query below, the LEFT() function is used to extract the country code from the PhoneNumber column records.

SELECT *, LEFT(PhoneNumber, 3) AS CountryCode 
FROM Employee;

This will produce the result as shown below:

PhoneNumberEmpIDAddressCountryCode
+33-14XXXXX011Grenelle, Paris, France+33
+31-20XXXXX192Geuzenveld, Amsterdam, Netherlands+31
+86-10XXXXX4583Yizhuangzhen, Beijing, China+86
+65-67XXXXX44Yishun, Singapore+65
+81-35XXXXX725Koto City, Tokyo, Japan+81

❮ MariaDB Functions

5