How the LEFT() function works in Mariadb?

The LEFT() function is a string function that returns the leftmost part of a given string, up to a specified number of characters.

Posted on

The LEFT() function is a string function that returns the leftmost part of a given string, up to a specified number of characters. It can be used to extract a substring from the beginning of a string expression, such as a column value, a literal string, or a variable.

Syntax

The syntax of the LEFT() function is as follows:

LEFT(string_expression, length)

The string_expression is the string that you want to get the left part from. It can be any valid string expression in Mariadb.

The length is the number of characters that you want to return from the left of the string. It can be any positive integer value. If the length is larger than the length of the string, the function returns the whole string. If the length is zero or negative, the function returns an empty string.

The LEFT() function returns a string value that is the left part of the input string, up to the specified length. If the input string is NULL, the function returns NULL.

Examples

In this section, we will show some examples of how to use the LEFT() function in Mariadb.

Example 1: Getting the left part of a literal string

The following example shows how to use the LEFT() function to get the left part of a literal string, up to a certain number of characters.

SELECT LEFT('Hello World!', 5);

The output is:

Hello

As you can see, the LEFT() function returns the leftmost 5 characters of the input string, which is Hello.

Example 2: Getting the left part of a column value

The following example shows how to use the LEFT() function to get the left part of a column value, up to a certain number of characters. Suppose we have a table called customers that stores the customer information, such as name, email, and country. The table has the following data:

id name email country
1 Alice [email protected] USA
2 Bob [email protected] UK
3 Charlie [email protected] Canada
4 David [email protected] Australia

We can use the LEFT() function to get the first 3 letters of the name column, as shown below:

SELECT id, LEFT(name, 3) AS name, email, country FROM customers;

The output is:

id name email country
1 Ali [email protected] USA
2 Bob [email protected] UK
3 Cha [email protected] Canada
4 Dav [email protected] Australia

As you can see, the LEFT() function returns the leftmost 3 characters of the name column for each row.

Example 3: Getting the left part of a variable value

The following example shows how to use the LEFT() function to get the left part of a variable value, up to a certain number of characters. Suppose we have a variable called @greeting that stores a greeting message, such as “Hello World!”. We can use the LEFT() function to get the first 4 letters of the variable value, as shown below:

SET @greeting = 'Hello World!';
SELECT LEFT(@greeting, 4);

The output is:

Hell

As you can see, the LEFT() function returns the leftmost 4 characters of the variable value, which is Hell.

Example 4: Getting the left part of a string with special characters

The following example shows how to use the LEFT() function to get the left part of a string with special characters, up to a certain number of characters. Suppose we have a string that contains some uppercase letters, some lowercase letters, some numbers, some symbols, and some accented characters, such as “A1B2C3!@#$%^&*()ÉÈÊË”. We can use the LEFT() function to get the first 10 characters of the string, as shown below:

SELECT LEFT('A1B2C3!@#$%^&*()ÉÈÊË', 10);

The output is:

A1B2C3!@#$

As you can see, the LEFT() function returns the leftmost 10 characters of the input string, which is A1B2C3!@#$.

There are some other functions that are related to the LEFT() function in Mariadb. They are:

  • The RIGHT() function: This function returns the rightmost part of a given string, up to a specified number of characters. It is the opposite of the LEFT() function. For example, RIGHT('Hello World!', 6) returns World!.
  • The SUBSTRING() function: This function extracts a substring from a given string, starting from a specified position and optionally with a specified length. It is more flexible than the LEFT() and RIGHT() functions. For example, SUBSTRING('Hello World!', 7, 5) returns World.
  • The LENGTH() function: This function returns the length of a given string in bytes. It can be used to get the length of the input string for the LEFT() and RIGHT() functions. For example, SELECT LEFT('Hello World!', LENGTH('Hello World!')) returns Hello World!.

Conclusion

In this article, we have learned how the LEFT() function works in Mariadb. We have seen the syntax of the function, and some examples of how to use it with different types of string expressions. We have also learned about some related functions that can be used with the LEFT() function. The LEFT() function is a useful function that can help us manipulate strings in Mariadb.