LENGTH

Purpose

This function returns the length of a string in characters.

Syntax

length::=

Usage notes

  • LENGTH returns the stored string length. This means that the returned result can be different depending on whether the string is of VARCHAR or CHAR type.

    • CHAR(n) has a fixed and pre-defined length n. If you insert a value shorter than n, space characters are automatically added to fill the remaining space (padding).

    • VARCHAR(n) can contain a string of the length n or 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
Copy
SELECT LENGTH('abc') LENGTH;
Example 2 - Different results for CHAR and VARCHAR:
Copy
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