LEFT

The LEFT function returns the leftmost integer characters of expression.

LEFT(expression,integer)

If expression is a character string, the result is a character string. If expression is a graphic string, the result is a graphic string. If expression is a binary string, the result is a binary string.

expression
An expression that specifies the string from which the result is derived. The arguments must be expressions that return a value of any built-in numeric, character string, graphic string, or a binary string data type. A numeric argument is cast to a character string before evaluating the function. For more information about converting numeric to a character string, see VARCHAR.

A substring of expression is zero or more contiguous characters of expression. If expression is a character string or graphic string, a single character is either an SBCS, DBCS, or multiple-byte character. If expression is a binary string, the result is the number of bytes in the argument.

integer
An expression that returns a built-in integer data type. The integer specifies the length of the result. The value of integer must be greater than or equal to 0 and less than or equal to n, where n is the length attribute of expression.

The result of the function is a varying-length string with a length attribute that is the same as the length attribute of expression and a data type that depends on the data type of expression:

Data type of expression Data type of the Result
CHAR or VARCHAR VARCHAR
CLOB CLOB
GRAPHIC or VARGRAPHIC VARGRAPHIC
DBCLOB DBCLOB
BINARY or VARBINARY VARBINARY
BLOB BLOB

The actual length of the result is integer.

If any argument can be null, the result can be null; if any argument is null, the result is the null value.

The CCSID of the result is the same as that of expression.

Example

  • Assume the host variable NAME (VARCHAR(50)) has a value of 'KATIE AUSTIN' and the host variable FIRSTNAME_LEN (int) has a value of 5.
      SELECT LEFT(:NAME, :FIRSTNAME_LEN)
        FROM SYSIBM.SYSDUMMY1
    Returns the value 'KATIE'
  • Assume that NAME is a VARCHAR(128) column, encoded in Unicode UTF-8, that contains the value 'Jürgen'.
      SELECT LEFT(NAME, 2), SUBSTR(NAME, 1, 2)
        FROM T1
        WHERE NAME = 'Jürgen'
    Returns the value 'Jü' for LEFT and 'JÊ' for SUBSTR(NAME, 1, 2).