length() takes a character string argument and returns an integer, which represents the number of bytes in its argument, ignoring any blank spaces after the character string.
length() displays a dialog box with a field, into which you can type a string. The string length is then measured and a message window confirms the string and tells you, by returning an integer value, how many characters it contains.
In this example, the length() results are displayed in the form message string.
MAIN
DEFINE str CHAR(20)
OPEN WINDOW w1 WITH FORM "length_function"
INPUT BY NAME str
ON ACTION ACCEPT
LET str = fgl_dialog_getbuffer()
MESSAGE "length()=", length(str)
END INPUT
CLOSE WINDOW w1
END MAIN
DATABASE FORMONLY
SCREEN
{
Enter a string and press
ACCEPT button:
[f1 ]
}
ATTRIBUTES
f1=FORMONLY.str;
INSTRUCTIONS
SCREEN RECORD s_rec (
FORMONLY.str
)
DELIMITERS "[]"
These are some of the uses that the LENGTH( ) function can be used for:
length() is also useful as a check on user input. In the following example, an IF statement is used to determine whether the user has responded to a displayed message:
IF LENGTH (l_contact.cont_name) = 0 THEN
NEXT FIELD cont_name
ERROR "You must enter a value for this field" ELSE ...
If its argument is a null string, LENGTH( ) returns zero.
length() is one of the 4GL built-in functions that can also be used in SQL statements. It can also be called from a C function.
In a SELECT or UPDATE statement, the argument of length() is the identifier of a character column. In this context, length() returns the number of bytes in the CLIPPED data value (for CHAR and VARCHAR columns) or the full number of bytes (for TEXT and BYTE data types).
length() can also take the name of a database column as its argument but only within an SQL statement.