DB2 Version 10.1 for Linux, UNIX, and Windows

LENGTH scalar function

The LENGTH function returns the length of expression in the implicit or explicit string unit.

Read syntax diagramSkip visual syntax diagram
>>-LENGTH--(--expression--+--------------------+--)------------><
                          '-,--+-CODEUNITS16-+-'      
                               +-CODEUNITS32-+        
                               '-OCTETS------'        

The schema is SYSIBM.

expression
An expression that returns a value that is a built-in data type. If expression can be null, the result can be null; if expression is null, the result is the null value.
CODEUNITS16, CODEUNITS32, or OCTETS
Specifies the string unit of the result. CODEUNITS16 specifies that the result is to be expressed in 16-bit UTF-16 code units. CODEUNITS32 specifies that the result is to be expressed in 32-bit UTF-32 code units. OCTETS specifies that the result is to be expressed in bytes.

If a string unit is explicitly specified, and if expression is not string data, an error is returned (SQLSTATE 428GC). 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".

If a string unit is not explicitly specified, the data type of the result determines the unit that is used. If the result is graphic data, the value returned specifies the length in 2-byte units. 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:

Examples