LEFT

The LEFT function returns a string that consists of the specified number of leftmost bytes of the specified string units.

Character string:

Read syntax diagram
>>-LEFT(character-expression,length-+--------------------+-)---><
                                    '-,--+-CODEUNITS16-+-'     
                                         +-CODEUNITS32-+       
                                         '-OCTETS------'       

Graphic string:

Read syntax diagram
>>-LEFT(graphic-expression,length-+--------------------+-)-----><
                                  '-,--+-CODEUNITS16-+-'     
                                       '-CODEUNITS32-'       

Binary string:

Read syntax diagram
>>-LEFT(binary-expression,length)------------------------------><

The schema is SYSIBM.

The LEFT function returns the leftmost string of character-expression, graphic-expression, or binary-expression consisting of length of the string units that are specified implicitly or explicitly.

Character string:

character-expression
An expression that specifies the string from which the result is derived. The string must be a character string. A substring of character-expression is zero or more contiguous code points of character-expression.

The string can contain mixed data. Depending on the units that are specified to evaluate the function, the result is not necessarily a properly formed mixed data character string.

Start of changeThe argument can also be a numeric data type. The numeric argument is implicitly cast to a VARCHAR data type.End of change

length
An expression that specifies the length of the result. The value must be an integer between 0 and n, where n is the length attribute of character-expression, expressed in the units that are either implicitly or explicitly specified.

Start of changeThe argument can also be a character string or graphic string data type. The string input is implicitly cast to a numeric value of DECFLOAT(34) which is then assigned to an INTEGER value.End of change

If CODEUNITS16 or CODEUNITS32 is specified, see Determining the length attribute of the final result for information about how to calculate the length attribute of the result string.

CODEUNITS16, CODEUNITS32, or OCTETS
Specifies the unit that is used to express length. If character-expression is defined as bit data, CODEUNITS16 and CODEUNITS32 cannot be specified.
CODEUNITS16
Specifies that length is expressed in terms of 16-bit UTF-16 code units.
CODEUNITS32
Specifies that length is expressed in terms of 32-bit UTF-32 code units.
OCTETS
Specifies that length is expressed in terms of bytes.

For more information about CODEUNITS16, CODEUNITS32, and OCTETS, see String unit specifications.

Graphic string:

graphic-expression
An expression that specifies the string from which the result is derived. The string must be a graphic string. A substring of graphic-expression is zero or more contiguous code points of graphic-expression. Start of changeA partial surrogate character in the expression is replaced with a blank.End of change

Start of changeThe argument can also be a numeric data type. The numeric argument is implicitly cast to a VARCHAR data type.End of change

length
An expression that specifies the length of the result. The value must be an integer between 0 and n, where n is the length attribute of graphic-expression, expressed in the units that are either implicitly or explicitly specified.

Start of changeThe argument can also be a character string or graphic string data type. The string input is implicitly cast to a numeric value of DECFLOAT(34) which is then assigned to an INTEGER value.End of change

If CODEUNITS16 or CODEUNITS32 is specified, see Determining the length attribute of the final result for information about how to calculate the length attribute of the result string.

CODEUNITS16 or CODEUNITS32
Specifies the unit that is used to express length.
CODEUNITS16
Specifies that length is expressed in terms of 16-bit UTF-16 code units.
CODEUNITS32
Specifies that length is expressed in terms of 32-bit UTF-32 code units.

For more information about CODEUNITS16 and CODEUNITS32, see String unit specifications.

Binary string:

binary-expression
An expression that specifies the string from which the result is derived. The string must be a binary string. A substring of binary-expression is zero or more contiguous code points of binary-expression.
length
An expression that specifies the length of the result. The value must be an integer between 0 and n, where n is the length attribute of binary-expression, expressed in the units that are either implicitly or explicitly specified.

The character-expression, graphic-expression, or binary-expression is effectively padded on the right with the necessary number of padding characters so that the specified substring of the expression always exists. The encoding scheme of the data determines the padding character:

  • For ASCII SBCS data or ASCII mixed data, the padding character is X'20'.
  • For ASCII DBCS data, the padding character depends on the CCSID; for example, for Japanese (CCSID 301) the padding character is X'8140', while for simplified Chinese it is X'A1A1'.
  • For EBCDIC SBCS data or EBCDIC mixed data, the padding character is X'40'.
  • For EBCDIC DBCS data, the padding character is X'4040'.
  • For Unicode SBCS data or UTF-8 (Unicode mixed data), the padding character is X'20'.
  • For UTF-16 (Unicode DBCS) data, the padding character is X'0020'.
  • For binary data, the padding character is X'00'.

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

  • VARCHAR if character-expression is CHAR or VARCHAR
  • CLOB if character-expression is CLOB
  • VARGRAPHIC if graphic-expression is GRAPHIC or VARGRAPHIC
  • DBCLOB if graphic-expression is DBCLOB
  • VARBINARY if binary-expression is BINARY or VARBINARY
  • BLOB if binary-expression is BLOB

The actual length of the result is determined from length.

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 the first expression.

Example 1: Assume that host variable ALPHA has a value of 'ABCDEF'. The following statement returns 'ABC', which are the three leftmost characters in ALPHA:
  SELECT LEFT(:ALPHA,3)
    FROM SYSIBM.SYSDUMMY1;
Example 2: Assume that host variable NAME, which is defined as VARCHAR(50), has a value of 'KATIE AUSTIN' and the integer host variable FIRSTNAME_LEN has a value of 5. The following statement returns the value 'KATIE':
   SELECT LEFT(:NAME, :FIRSTNAME_LEN)
     FROM SYSIBM.SYSDUMMY1;
Example 3: The following statement returns a zero length string.
   SELECT LEFT('ABCABC',0)
     FROM SYSIBM.SYSDUMMY1;
Example 4: The FIRSTNME column in sample EMP table is defined as VARCHAR(12). Find the first name for an employee whose last name is 'BROWN' and return the first name in a 10-byte string.
   SELECT LEFT(FIRSTNME,10)
     FROM DSN8A10.EMP
     WHERE LASTNAME='BROWN';
This function returns a VARCHAR(10) string that has the value of 'DAVID' followed by 5 blank characters.
Example 5: FIRSTNAME is a VARCHAR(12) column in table T1. One of its values is the 6-character string 'Jürgen'. When FIRSTNAME has this value:
   Function ...                            Returns ...
   LEFT(FIRSTNAME,2,CODEUNITS32)           'Jü'  -- x'4AC3BC'
   LEFT(FIRSTNAME,2,CODEUNITS16)           'Jü'  -- x'4AC3BC'
   LEFT(FIRSTNAME,2,OCTETS)                'J '  -- x'4A20' a truncated string