The Open Query File (OPNQRYF) command's wild card (%WLDCRD) function is great for selecting records based on the contents of character fields, but sometimes you have to go to extra lengths to make it do what you want. For example, suppose you have an inventory file with item numbers of one to six characters. If you want to select all items that start with 1T, you can use the following OPNQRYF command:
OPNQRYF FILE((ITEMPF)) + QRYSLT('ITEM = %WLDCRD("1T*")')
If you want the items that have 1T anywhere in the item number, you would change the QRYSLT parameter to this:
QRYSLT('ITEM = %WLDCRD("*1T*")')
or
QRYSLT('ITEM *CT "1T"')
What if you want the items whose item numbers end in 1T? You might think this would do it:
QRYSLT('ITEM = %WLDCRD("*1T")')
This will work if all item numbers use the full six bytes. But if there are shorter item numbers, such as 4X1T, this query selection expression won't work. To retrieve shorter item numbers is a little messy but not difficult. The trick is to add some blanks to both parts of the expression. The minimum number of blanks is the length of the field minus the number of matching characters in the wild card. In this case, the minimum number of blanks is four, because the item number is six characters long and 1T is two characters long. The safe way to figure out how many characters to add is to just use the length of the field. There is one additional requirement. You have to add an extra "match-all" character (usually an asterisk) to the end of the wild card. The query select parameter looks like this:
QRYSLT('ITEM *CAT " " = + %WLDCRD("*1T *")')
This expression will select items such as 1T, 4X1T, and 53351T. To see how it works, consider the case in which ITEM has the value 4X1T. The left side of the expression is 4X1T followed by eight trailing blanks. The characters 4X match the first asterisk in the wild card. The characters 1T and the following six blanks match exactly. The remaining two blanks match the last * in the wild card.
- Ted Holt
LATEST COMMENTS
MC Press Online