Finding the length of data items

You can use the LENGTH function in many contexts (including tables and numeric data) to determine the length of an item. For example, you can use the LENGTH function to determine the length of an alphanumeric or national literal, or a data item of any type except DBCS.Start of change You can also use the BYTE-LENGTH function to determine the length of an item in bytes.End of change

LENGTH intrinsic function

The LENGTH function returns the length of a national item (a literal, or any item that has USAGE NATIONAL, including national group items) as an integer equal to the length of the argument in national character positions. It returns the length of any other data item as an integer equal to the length of the argument in alphanumeric character positions.

The following COBOL statement demonstrates moving a data item into the field in a record that holds customer names:

Move Customer-name To Customer-record(1:Function Length(Customer-name))

Start of changeBYTE-LENGTH intrinsic functionEnd of change

Start of change The BYTE-LENGTH function returns the length of a national item, an alphanumeric item, or a DBCS literal as an integer equal to the length of the argument in bytes. End of change

LENGTH OF special register

You can also use the LENGTH OF special register, which returns the length in bytes even for national data. Coding either Function Length(Customer-name) or LENGTH OF Customer-name returns the same result for alphanumeric items: the length of Customer-name in bytes. Start of changeThe BYTE-LENGTH function returns the same result as the LENGTH OF special register for all arguments types.End of change

You can use the Start of changeLENGTH and BYTE-LENGTH functionsEnd of change only where arithmetic expressions are allowed. However, you can use the LENGTH OF special register in a greater variety of contexts. For example, you can use the LENGTH OF special register as an argument to an intrinsic function that accepts integer arguments. (You cannot use an intrinsic function as an operand to the LENGTH OF special register.) You can also use the LENGTH OF special register as a parameter in a CALL statement.

related references    
Start of changeBYTE-LENGTH (Enterprise COBOL for z/OS® Language Reference)End of change
LENGTH (Enterprise COBOL for z/OS Language Reference)  
LENGTH OF (Enterprise COBOL for z/OS Language Reference)