length Function

The length function returns the length of a character string. The length function calculates the length using the UTF character set.

Syntax

returnvalue length(source)

source ::= any*
returnvalue ::= integer

Semantics

source

The input string for which the length should be determined. This argument is implicitly cast to a sequence of strings.

returnvalue

Returns NULL if the source argument is NULL.

Returns NULL if the source argument is an empty sequence or a sequence with more than one item.

Note:

Characters that are represented on 32 or more bits, the length is considered 1, while Java String.length() returns 2 for UTF32 chars, 4 for UTF64, etc.

Example 12-14 length Function

In this example, the length of the first name is selected from the users table.

SELECT firstname, length(firstname) as length FROM users;
 +-----------+--------+
 | firstname | length |
 +-----------+--------+
 | John      |      4 |
 | Mary      |      4 |
 | Peter     |      5 |
 +-----------+--------+