When the second parameter of %LEN is *MAX, this function returns the maximum number of characters for a varying-length expression. When the first parameter of %LEN is a field name, this value is the same as the defined length of the field. For example, if a variable-length UCS-2 field is defined as 25C, %LEN(fld:*MAX) returns 25.
D char_varying s 100a varying
D ucs2_varying s 5000c varying
D graph_varying s 7000g varying(4)
D graph_fld10 s 10g
D char_fld10 s 10a
/free
// Calculate several length and size values
// - The maximum length, %LEN(*MAX), measured in characters
// - The current length, %LEN, measured in characters
// - The size, %SIZE, measured in bytes, including the
// 2- or 4-byte length prefix
// Each alphanumeric character has one byte
char_varying = 'abc'; // Length is 3
max_len = %len(char_varying : *MAX);
len = %len(char_varying);
size = %size(char_varying);
// max_len = 100
// len = 3
// size = 102 (100 + 2)
// Each UCS-2 character has two bytes
ucs2_varying = 'abc'; // Length is 3
max_len = %len(ucs2_varying : *MAX);
len = %len(ucs2_varying);
size = %size(ucs2_varying);
// max_len = 5000
// len = 3
// size = 10002 (5000 * 2 + 4)
// Each graphic character has two bytes.
// For field graph_varying, VARYING(4) was specified,
// so the length prefix has four bytes
graph_varying = graph_fld10; // Length is 10
max_len = %len(graph_varying : *MAX);
len = %len(graph_varying);
size = %size(graph_varying);
// max_len = 7000
// len = 10
// size = 14004 (7000 * 2 + 4)
// Calculate %LEN(*MAX) of a concatenation
graph_varying = %subst(graph_fld10:1:5); // Length is 5
max_len = %len(graph_varying + graph_fld10 : *MAX);
len = %len(graph_varying + graph_fld10);
// max_len = 7010 (7000 + 10)
// len = 15 (5 + 10)
// Calculate %LEN(*MAX) of a %TRIM expression
char_fld10 = '1234'; // Trimmed length is 4
max_len = %len(%trim(char_fld10) : *MAX);
len = %len(%trim(char_fld10));
// max_len = 10 (maximum trimmed length)
// len = 4 (actual trimmed length)