How the LTRIM() function works in Mariadb?

The LTRIM() function is a string function that returns a string that is left-trimmed of any leading spaces.

Posted on

The LTRIM() function is a string function that returns a string that is left-trimmed of any leading spaces. The LTRIM() function is useful for removing any unwanted spaces from the beginning of a string.

Syntax

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

LTRIM(string)

The string argument is the string to be left-trimmed. The string argument can be any valid string expression, such as a column name, a literal value, a function call, etc.

The LTRIM() function returns a string value that is the left-trimmed version of the string argument. If the string argument is NULL, the function returns NULL.

Examples

Example 1: Basic usage of the LTRIM() function

The following example shows how to use the LTRIM() function with a simple string. It returns a string that is left-trimmed of any leading spaces.

SELECT LTRIM('   Hello');

The output is:

Hello

This means that the LTRIM() function removes all the spaces from the left of the string ’ Hello'.

Example 2: Using the LTRIM() function with a column name

The following example shows how to use the LTRIM() function with a column name. It returns the left-trimmed version of the values in the name column of the employees table.

SELECT LTRIM(name) FROM employees;

The output is:

Alice
Bob
Charlie
David
Eve

This means that the LTRIM() function removes any leading spaces from the values of the name column.

Example 3: Using the LTRIM() function with a function call

The following example shows how to use the LTRIM() function with a function call. It returns the left-trimmed version of the current user name.

SELECT LTRIM(USER());

The output is:

root@localhost

This means that the LTRIM() function removes any leading spaces from the result of the USER() function.

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

  • RTRIM(): This function returns a string that is right-trimmed of any trailing spaces. It is the opposite of the LTRIM() function, meaning that RTRIM(LTRIM(x)) = TRIM(x) for any string x.
  • TRIM(): This function returns a string that is trimmed of any leading or trailing spaces or a specified character. It is a combination of the LTRIM() and RTRIM() functions.
  • LPAD(): This function returns a string that is left-padded with a specified character to a certain length. It is similar to the LTRIM() function, except that it adds characters instead of removing them.
  • RPAD(): This function returns a string that is right-padded with a specified character to a certain length. It is similar to the RTRIM() function, except that it adds characters instead of removing them.

Here are some examples of using these related functions:

-- Get the right-trimmed version of 'Hello   '
SELECT RTRIM('Hello   ');

-- Get the trimmed version of '  Hello World  '
SELECT TRIM('  Hello World  ');

-- Get the left-padded version of '123' with '0' to a length of 5
SELECT LPAD('123', 5, '0');

-- Get the right-padded version of 'abc' with '*' to a length of 5
SELECT RPAD('abc', 5, '*');

Conclusion

In this article, we have learned how the LTRIM() function works in Mariadb. We have seen its syntax, examples, and related functions. We have also learned how to use the LTRIM() function to return a string that is left-trimmed of any leading spaces. The LTRIM() function is a useful function to remove any unwanted spaces from the beginning of a string.