LENGTH
Purpose
This function returns the length of a string in characters.
Syntax
length::=
Usage notes
-
LENGTHreturns the stored string length. This means that the returned result can be different depending on whether the string is ofVARCHARorCHARtype.-
CHAR(n)has a fixed and pre-defined lengthn. If you insert a value shorter thann, space characters are automatically added to fill the remaining space (padding). -
VARCHAR(n)can contain a string of the lengthnor smaller. The string is stored in its actual length without padding.
For an example of how the string data type can affect the result, see Example 2.
For more information about string data types, see Data type details.
-
-
CHARACTER_LENGTH is an alias of
LENGTH.
Examples
Example 1 - Get the character length of a string
SELECT LENGTH('abc') LENGTH;
Example 2 - Different results for CHAR and VARCHAR:
CREATE SCHEMA my_schema;
CREATE OR REPLACE TABLE my_schema.my_table (
c_fixed CHAR(10),
c_var VARCHAR(10)
);
INSERT INTO my_schema.my_table VALUES ('ABC', 'ABC');
SELECT
c_fixed,
c_var,
LENGTH(c_fixed),
LENGTH(c_var),
FROM my_schema.my_table
;
Output:
| C_FIXED | C_VAR | LENGTH(MY_TABLE.C_FIXED) | LENGTH(MY_TABLE.C_VAR) |
|---|---|---|---|
| ABC | ABC | 10 | 3 |