CHAR_LENGTH: Returning the Length in Characters of a String

How to:

The CHAR_LENGTH function returns the length, in characters, of a string. In Unicode environments, this function uses character semantics, so that the length in characters may not be the same as the length in bytes. If the string includes trailing blanks, these are counted in the returned length. Therefore, if the format source string is type An, the returned value will always be n.


Top of page

x
Syntax: How to Return the Length of a String in Characters
CHAR_LENGTH(source_string)

where:

source_string

Alphanumeric

Is the string whose length is returned.

The data type of the returned length value is Integer.



Example: Returning the Length of a String

The following request against the EMPLOYEE data source creates a virtual field named LASTNAME of type A15V that contains the LAST_NAME with the trailing blanks removed. It then uses CHAR_LENGTH to return the number of characters.

DEFINE FILE EMPLOYEE
LASTNAME/A15V = RTRIM(LAST_NAME);
END
TABLE FILE EMPLOYEE
SUM LAST_NAME NOPRINT AND COMPUTE
NAME_LEN/I3 = CHAR_LENGTH(LASTNAME);
BY LAST_NAME
ON TABLE SET PAGE NOPAGE
END

The output is:

   LAST_NAME        NAME_LEN    
  ---------        --------      
  BANNING                 7
  BLACKWOOD               9
  CROSS                   5
  GREENSPAN               9
  IRVING                  6
  JONES                   5
  MCCOY                   5
  MCKNIGHT                8
  ROMANS                  6
  SMITH                   5
  STEVENS                 7

WebFOCUS