Db2 RIGHT

Db2 RIGHT() function returns a substring that consists of the specified number of rightmost characters from a string.

The following shows the syntax of the RIGHT() function:

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

In this syntax:

  • First, specify the source string ( string) from which substring is extracted.
  • Second, specify the number of rightmost characters ( length) to be extracted.
  • Third, optionally specify the unit of the length with the value in the following table.
UnitDescription
 CODEUNITS16Specifies that the length is expressed in terms of 16-bit UTF-16 code units.
 CODEUNITS32Specifies that the length is expressed in terms of 32-bit UTF-32 code units.
 OCTETSSpecifies that the length is expressed in terms of bytes.

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

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

The following shows the output:

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

The following example illustrates how to use the RIGHT() function with a unit:

SELECT RIGHT('Jürgen',5,CODEUNITS16) FROM sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

This is the output:

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

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

Was this tutorial helpful ?