The CHARACTER_LENGTH function returns the length of expression in the specified string unit.
>>-+-CHARACTER_LENGTH-+-----------------------------------------> '-CHAR_LENGTH------' >----(--expression--,--+-CODEUNITS16-+--)---------------------->< +-CODEUNITS32-+ '-OCTETS------'
The schema is SYSIBM.
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".
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 varying-length strings is the actual length and not the maximum length.
SELECT CHARACTER_LENGTH(NAME, CODEUNITS32)
FROM T1 WHERE NAME = 'Jürgen'
SELECT CHARACTER_LENGTH(NAME, CODEUNITS16)
FROM T1 WHERE NAME = 'Jürgen'
The following
two queries return the value 7: SELECT CHARACTER_LENGTH(NAME, OCTETS)
FROM T1 WHERE NAME = 'Jürgen'
SELECT LENGTH(NAME)
FROM T1 WHERE NAME = 'Jürgen'
| '&' | 'N' | '~' | 'A' | 'B' | |
|---|---|---|---|---|---|
| UTF-8 | X'F09D849E' | X'4E' | X'CC83' | X'41' | X'42' |
| UTF-16BE | X'D834DD1E' | X'004E' | X'0303' | X'0041' | X'0042' |
| UTF-32BE | X'0001D11E' | X'0000004E' | X'00000303' | X'00000041' | X'00000042' |
SELECT CHARACTER_LENGTH(UTF8_VAR, CODEUNITS16),
CHARACTER_LENGTH(UTF8_VAR, CODEUNITS32),
CHARACTER_LENGTH(UTF8_VAR, OCTETS)
FROM SYSIBM.SYSDUMMY1
returns the values 6,
5, and 9, respectively. SELECT CHARACTER_LENGTH(UTF16_VAR, CODEUNITS16),
CHARACTER_LENGTH(UTF16_VAR, CODEUNITS32),
CHARACTER_LENGTH(UTF16_VAR, OCTETS)
FROM SYSIBM.SYSDUMMY1
returns the values 6,
5, and 12, respectively.