Skip to main content

CHAR_LENGTH (SQL)

A function that returns the number of characters in an expression.

Synopsis

CHAR_LENGTH(expression)

Description

CHAR_LENGTH returns an integer value representing the number of characters, not the number of bytes, in the specified expression. The expression can be a string, or any other data type such as a numeric or a data stream field. This integer count returned including leading and trailing blanks and the string-termination character. CHARACTER_LENGTH returns NULL if passed a NULL value, and 0 if passed an empty string ('') value.

Numbers are parsed to canonical form before counting the characters; quoted number strings are not parsed. In the following example, the first CHAR_LENGTH returns 1 (because number parsing removes leading and trailing zeros), the second CHAR_LENGTH returns 8.

SELECT CHAR_LENGTH(007.0000) AS NumLen,
       CHAR_LENGTH('007.0000') AS NumStringLen
Note:

The CHAR_LENGTH, CHARACTER_LENGTH, and DATALENGTH functions are identical. All of them accept a stream field argument. The LENGTH and $LENGTH functions do not accept a stream field argument.

LENGTH also differs from these functions by stripping trailing blanks and the string-termination character before counting characters.

$LENGTH also differs from these functions because it returns 0 if passed a NULL value, and 0 if passed an empty string. $LENGTH differs from the other length function by returning data type SMALLINT; all the other length functions return data type INTEGER.

Arguments

expression

An expression, which can be the name of a column, a string literal, or the result of another scalar function. The underlying data type can be a character type (such as CHAR or VARCHAR), a numeric, or a data stream.

CHAR_LENGTH returns the INTEGER data type.

Examples

The following example returns the number of characters in the state abbreviation field (Home_State) in the Sample.Employee table. (All U.S. states have a two-letter postal abbreviation):

SELECT DISTINCT CHAR_LENGTH(Home_State) AS StateLength
     FROM Sample.Employee

The following example returns the names of the employees and the number of characters in each employee name, ordered by ascending number of characters:

SELECT Name,
     CHAR_LENGTH(Name) AS NameLength
     FROM Sample.Employee
     ORDER BY NameLength

The following examples return the number of characters in a character stream field (Notes) and a binary stream field (Picture) in the Sample.Employee table:

SELECT DISTINCT CHAR_LENGTH(Notes) AS NoteLen
     FROM Sample.Employee WHERE Notes IS NOT NULL
SELECT DISTINCT CHAR_LENGTH(Picture) AS PicLen
     FROM Sample.Employee WHERE Picture IS NOT NULL

The following Embedded SQL example shows how CHAR_LENGTH handles Unicode characters. CHAR_LENGTH counts the number of characters, regardless of their byte length:

    SET a=$CHAR(960)_"FACE"
    WRITE !,a
    &sql(SELECT CHAR_LENGTH(:a) INTO :b)
    IF SQLCODE'=0 {WRITE !,"Error code ",SQLCODE }
    ELSE {WRITE !,"The CHAR length is ",b }

returns 5.

See Also

FeedbackOpens in a new tab