How the LCASE() function works in Mariadb?

The LCASE() function is a string function that converts all the characters in a given string to lowercase.

Posted on

The LCASE() function is a string function that converts all the characters in a given string to lowercase. It is equivalent to the LOWER() function and can be used interchangeably. The LCASE() function can be applied to any string expression, such as a column name, a literal string, or a variable.

Syntax

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

LCASE(string_expression)

The string_expression is the string that you want to convert to lowercase. It can be any valid string expression in Mariadb.

The LCASE() function returns a string value that is the lowercase version of the input string. If the input string is NULL, the function returns NULL.

Examples

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

Example 1: Converting a literal string to lowercase

The following example shows how to use the LCASE() function to convert a literal string to lowercase.

SELECT LCASE('Hello World!');

The output is:

hello world!

As you can see, the LCASE() function converts all the characters in the input string to lowercase.

Example 2: Converting a column value to lowercase

The following example shows how to use the LCASE() function to convert a column value to lowercase. 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 LCASE() function to convert the name column to lowercase, as shown below:

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

The output is:

id name email country
1 alice [email protected] USA
2 bob [email protected] UK
3 charlie [email protected] Canada
4 david [email protected] Australia

As you can see, the LCASE() function converts the name column to lowercase.

Example 3: Converting a variable value to lowercase

The following example shows how to use the LCASE() function to convert a variable value to lowercase. Suppose we have a variable called @greeting that stores a greeting message, such as “Hello World!”. We can use the LCASE() function to convert the variable value to lowercase, as shown below:

SET @greeting = 'Hello World!';
SELECT LCASE(@greeting);

The output is:

hello world!

As you can see, the LCASE() function converts the variable value to lowercase.

Example 4: Converting a string with special characters to lowercase

The following example shows how to use the LCASE() function to convert a string with special characters to lowercase. 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 LCASE() function to convert the string to lowercase, as shown below:

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

The output is:

a1b2c3!@#$%^&*()éèêë

As you can see, the LCASE() function converts all the uppercase letters to lowercase, while leaving the other characters unchanged.

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

  • The UCASE() function: This function converts all the characters in a given string to uppercase. It is equivalent to the UPPER() function and can be used interchangeably. For example, UCASE('Hello World!') returns HELLO WORLD!.
  • The CONCAT() function: This function concatenates two or more strings into one string. For example, CONCAT('Hello', ' ', 'World!') returns Hello World!.
  • The SUBSTRING() function: This function extracts a substring from a given string. For example, SUBSTRING('Hello World!', 1, 5) returns Hello.
  • The LENGTH() function: This function returns the length of a given string in bytes. For example, LENGTH('Hello World!') returns 12.

Conclusion

In this article, we have learned how the LCASE() 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 LCASE() function. The LCASE() function is a useful function that can help us manipulate strings in Mariadb.