DB2 Scalar functions - LENGTH
The LENGTH function returns the length of expression in the implicit or explicit string unit.
The schema is SYSIBM.
If a string unit is explicitly specified, and if expression is not string data, an error is returned (SQLSTATE 428F5). If a string unit is specified as CODEUNITS16 or CODEUNITS32, and expression is a binary string or bit data, an error is returned (SQLSTATE 428GC). If a string unit is specified as OCTETS and expression is a binary string, an error is returned (SQLSTATE 42815). 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
If a string unit argument is not explicitly specified and if expression is a character or graphic string, the string units of expression determines the string unit that is used for the result. Otherwise, the value returned specifies the length in bytes.
The result of the function is a large integer. If the argument can be null, the result can be null; if the argument is null, the result is the null value.
The length of character and graphic strings includes trailing blanks. The length of binary strings includes binary zeros. The length of varying-length strings is the actual length and not the maximum length. The length of all other values is the number of bytes used to represent the value:
LENGTH(:ADDRESS)
LENGTH(START_DATE)
LENGTH(CHAR(START_DATE, EUR))
Assume that the variable UTF8_VAR contains the UTF-8 representation of the string.
SELECT LENGTH(UTF8_VAR, CODEUNITS16) , LENGTH(UTF8_VAR, CODEUNITS32) , LENGTH(UTF8_VAR, OCTETS) FROM SYSIBM.SYSDUMMY1
Assume that the variable UTF16_VAR contains the UTF-16BE representation of the string.
SELECT LENGTH(UTF16_VAR, CODEUNITS16) , LENGTH(UTF16_VAR, CODEUNITS32) , LENGTH(UTF16_VAR, OCTETS) FROM SYSIBM.SYSDUMMY1