Db2 LTRIM

Summary: in this tutorial, you will learn how to use the LTRIM() function to remove specific characters from the beginning of a string.

Db2 LTRIM() function overview

The following illustrates the syntax LTRIM() function:

LTRIM(input_string, [trim_string])
Code language: SQL (Structured Query Language) (sql)

The LTRIM() function removes all characters contained in the trim_string from the beginning of the input_string.

If you don’t specify the trim_string argument, the LTRIM() function will remove the leading blanks from the input_string.

Note that Db2 uses the binary representation of each character in the trim_string to compare with the bytes at the beginning of the input_string.

Db2 LTRIM() function examples

Let’s take some examples of using the LTRIM() function.

1) Using LTRIM() function to remove leading blanks from a string example

This example uses the LTRIM() function to remove leading blanks from a the string ' Db2 LTRIM':

SELECT LTRIM(' Db2 LTRIM') result FROM SYSIBM.SYSDUMMY1;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

RESULT ------------ Db2 LTRIM
Code language: SQL (Structured Query Language) (sql)

2) Using LTRIM() function to remove a specific string from the beginning of a string example

The following example uses the LTRIM() function to remove the string 'abc' from the string 'abc123' string:

SELECT LTRIM('abc123','abc') result FROM SYSIBM.SYSDUMMY1;
Code language: SQL (Structured Query Language) (sql)

The following illustrates the output:

RESULT ------ 123
Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to use the Db2 LTRIM() function to remove a specified string from the beginning of a string.

Was this tutorial helpful ?