The LENGTH function
The LENGTH function returns the number of bytes of data in character data.
However, the behavior of the LENGTH function varies with the type of argument that the user specifies. The argument can be a quoted string, a character-type column other than the TEXT data type, a TEXT column, a host variable, or an SPL routine variable.
The
following table shows how the LENGTH function operates on each of
these argument types. The Example column in this table uses
the symbol s
to represent a single-byte trailing
white space character.
This table assumes that all arguments consist of single-byte characters.
LENGTH argument | Behavior | Example |
---|---|---|
Quoted string | Returns number of bytes in string, minus any trailing white space (as defined in the locale). | If the string is 'Ludwig', the result is 6. If the string is 'Ludwigssss', the result is still 6. |
CHAR, VARCHAR, LVARCHAR, NCHAR, or NVARCHAR column | Returns number of bytes in a column, minus any trailing white- space characters, regardless of defined length of the column. | If the fname column of the customer table is a CHAR(15) column, and this column contains the string 'Ludwig', the result is 6. If the fname column contains the string 'Ludwigssss', the result is still 6. |
TEXT column | Returns number of bytes in a column, including trailing white space characters. | If the cat_descr column in the catalog table is a TEXT column, and this column contains the string 'Ludwig', the result is 6. If the cat_descr column contains the string 'Ludwigssss', the result is 10. |
Host or procedure variable | Returns number of bytes that the variable contains, minus any trailing white pace, regardless of defined length of the variable. | If the procedure variable f_name is defined as CHAR(15), and this variable contains the string 'Ludwig', the result is 6. If the f_name variable contains the string 'Ludwigssss', the result is still 6. |
106
.SELECT lname AS cust_name,
length (fname) AS length, customer_num AS cust_num
FROM customer WHERE customer_num < 106
The following example of output shows the result of the query. For each row that is retrieved, the length column seems to show the number of characters in the lname (cust_name) column. However, the length column actually displays the number of bytes in the lname column.
cust_name | length | cust_num |
---|---|---|
Ludwig | 6 | 101 |
Carole | 6 | 102 |
Philip | 6 | 103 |
Anthony | 7 | 104 |
Raymond | 7 | 105 |
When you use the LENGTH function in a locale that supports a multibyte code set, such as the Japanese SJIS code set, the distinction between characters and bytes is meaningful. When you use the LENGTH function in a locale that supports a multibyte code set, the distinction between characters and bytes is meaningful.LENGTH returns the number of bytes in its argument. This result might be different from the number of characters.
SELECT lname AS cust_name,
length (fname) AS length, customer_num AS cust_num
FROM customer_multi WHERE customer_num = 199
aA1A2bB1B2
In
this representation, the first character (the symbol a
)
is a single-byte character. The second character (the symbol A1A2
)
is a 2-byte character. The third character (the symbol b
)
is a single-byte character. The fourth character (the symbol B1B2
)
is a 2-byte character.
cust_name | length | cust_num |
---|---|---|
aA1A2bB1B2 | 6 | 199 |