INCLUDE Substring Search


A substring search allows us to filter records by looking for specific substrings within a larger character field. DFSORT treats a substring as a portion of a field within a record that we can isolate based on its starting position and length. Substring searches are performed using the INCLUDE statements, focusing on a specific portion of a field.

Syntax -

//SYSIN DD * 
    SORT FIELDS=COPY
    INCLUDE COND=(field1_starting_position, field1_length, SS,
		 relational_operator, substring_constant)
/*
field1_starting_positionSpecifies the starting position of field1 to be compared
field1_lengthSpecifies the length of the field1 to be compared
SSSpecifies the search and filtering should be done based on the substring_constant.
relational_operatorused for condition comparison. The available operators are -
EQ 		Equal to
NE 		Not equal to
GT 		Greater than
GE 		Greater than or equal to
LT 		Less than
LE 		Less than or equal to
substring_constantSpecifies the substring used to filter.

Examples -


Scenario1 - Basic substring search.

INCLUDE COND=(10,2,CH,SS,C'HI')

This statement includes records where the 2-character field starting at position 10 equals 'HI'. It looks at only the 2 bytes starting at position 10 in the record, rather than comparing the entire field.

Scenario2 - Using multiple substring conditions with AND

INCLUDE COND=(5,3,CH,SS,C'XYZ',AND,20,4,CH,SS,C'ABCD')

This includes records where - the substring starting at position 5 (3 bytes long) is equal to 'XYZ', and the substring starting at position 20 (4 bytes long) is equal to 'ABCD'. Both conditions must be true for a record to be included.

Scenario3 - Substring search with OR

INCLUDE COND=(1,2,CH,SS,C'AA',OR,10,3,CH,SS,C'999')

This example includes records where - the substring starting at position 1 (2 bytes) is equal to 'AA', or the substring starting at position 10 (3 bytes) is equal to '999'. If either condition is true, the record is included.