Db2 LEFT

Db2 LEFT() function returns a substring that consists of a specified number of leftmost characters from a string.

Here is the syntax of the LEFT() function:

LEFT(string, length);
Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • First, specify the source string ( string) from which to extract the substring.
  • Second, specify the number of leftmost characters ( length) to be extracted.

This example returns the first three characters of the string 'IBM Db2':

SELECT LEFT('IBM Db2',3) result FROM sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

The following shows the output:

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

See the following authors tables from the sample database:

authors

This example uses the LEFT() function shows the number of the last names for each initial (A, B, C…):

SELECT LEFT(last_name,1) Initial, COUNT(last_name) FROM authors GROUP BY LEFT(last_name,1);
Code language: SQL (Structured Query Language) (sql)

The output is as follows:

db2 left function

In this tutorial, you have learned how to use the Db2 LEFT() function to return the number of leftmost characters of a string.

Was this tutorial helpful ?