DB2 Scalar functions - LEFT
The LEFT function returns the leftmost string of string-expression of length length, expressed in the specified string unit.
The schema is SYSIBM. The SYSFUN version of the LEFT function continues to be available.
The string-expression is padded on the right with the necessary number of padding characters so that the specified substring of string-expression always exists. The character used for padding is the same character that is used to pad the string in contexts where padding would occur. For more information about padding, see String assignments in Assignments and comparisons.
String assignments
Assignments and comparisons
An expression that specifies the length of the result. The expression must return a value that is a built-in numeric, CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type. If the value is not of type INTEGER, it is implicitly cast to INTEGER before evaluating the function. length must be greater than or equal to 0 (SQLSTATE 22011). If OCTETS is specified and the result is graphic data, the value must be an even number (SQLSTATE 428GC).
If length is not a constant and a string unit is not specified, length must be less than or equal to the length attribute of string-expression (SQLSTATE 22011).
If length is not a constant and a string unit is specified, length must be less than or equal to the corresponding value from the following table (SQLSTATE 22011):
L = length attribute of string-expression
If length is a constant and the data type of string-expression is:
CODEUNITS16 specifies that length is expressed in 16-bit UTF-16 code units. CODEUNITS32 specifies that length is expressed in 32-bit UTF-32 code units. OCTETS specifies that length is expressed in bytes.
If the string unit is specified as CODEUNITS16 or CODEUNITS32, and string-expression is a binary string or bit data, an error is returned (SQLSTATE 428GC). If the string unit is specified as OCTETS and string-expression is a graphic string, length must be an even number; otherwise, an error is returned (SQLSTATE 428GC). If a string unit is not explicitly specified, the string unit of string-expression determines the unit that is used. For more information about CODEUNITS16, CODEUNITS32, and OCTETS, see String units in built-in functions in Character strings.
String units in built-in functions
Character strings
The result of the function is a varying-length string that depends on the data type of string-expression:
If any argument can be null, the result can be null; if any argument is null, the result is the null value.
ABCDEF
SELECT LEFT(ALPHA,3) FROM SYSIBM.SYSDUMMY1
ABC
KATIE AUSTIN
SELECT LEFT(NAME , FIRSTNAME_LEN) FROM SYSIBM.SYSDUMMY1
KATIE
SELECT LEFT('ABCABC',0) FROM SYSIBM.SYSDUMMY1
BROWN
SELECT LEFT(FIRSTNME, 10) FROM EMPLOYEE WHERE LASTNAME = 'BROWN'
DAVID
Jürgen
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
&N~AB
&
~
N
A
B
Assume that the variable UTF8_VAR, with a length attribute of 20 bytes, contains the UTF-8 representation of the string.
SELECT LEFT(UTF8_VAR, 2, CODEUNITS16) , LEFT(UTF8_VAR, 2, CODEUNITS32) , LEFT(UTF8_VAR, 2, OCTETS) FROM SYSIBM.SYSDUMMY1
&N
bb
b
SELECT LEFT(UTF8_VAR, 5, CODEUNITS16) , LEFT(UTF8_VAR, 5, CODEUNITS32) , LEFT(UTF8_VAR, 5, OCTETS) FROM SYSIBM.SYSDUMMY1
&N~A
SELECT LEFT(UTF8_VAR, 10, CODEUNITS16) , LEFT(UTF8_VAR, 10, CODEUNITS32) , LEFT(UTF8_VAR, 10, OCTETS) FROM SYSIBM.SYSDUMMY1
&N~ABbbbb
&N~ABbbbbb
&N~ABb
Assume that the variable UTF16_VAR, with a length attribute of 20 code units, contains the UTF-16BE representation of the string.
SELECT LEFT(UTF16_VAR, 2, CODEUNITS16) , LEFT(UTF16_VAR, 2, CODEUNITS32) , HEX (LEFT(UTF16_VAR, 2, OCTETS)) FROM SYSIBM.SYSDUMMY1
SELECT LEFT(UTF16_VAR, 5, CODEUNITS16) , LEFT(UTF16_VAR, 5, CODEUNITS32) , LEFT(UTF16_VAR, 6, OCTETS) FROM SYSIBM.SYSDUMMY1
SELECT LEFT(UTF16_VAR, 10, CODEUNITS16) , LEFT(UTF16_VAR, 10, CODEUNITS32) , LEFT(UTF16_VAR, 10, OCTETS) FROM SYSIBM.SYSDUMMY1