Created On:  02 January 2011
Problem:
When using LIKE in a WHERE clause that references a column type of VARCHAR or NVARCHAR the correct rows are returned if a literal is used in the comparison but not if a host variable is used.
Example:
This works:
EXEC SQL
DECLARE mycursor CURSOR FOR SELECT DISTINCT
A.EMPL_NO
,A.EMP_NAME
FROM EMPLOYEES A
WHERE ( A.EMP_NAME LIKE N'%JAMILA%' )
END-EXEC
This fails:
MOVE N'%JAMILA%' to EMP-NAME
EXEC SQL
DECLARE mycursor CURSOR FOR SELECT DISTINCT
A.EMPL_NO
,A.EMP_NAME
FROM EMPLOYEES A
WHERE ( A.EMP_NAME LIKE :EMP-NAME )
END-EXEC
Why?
Example:
This works:
EXEC SQL
DECLARE mycursor CURSOR FOR SELECT DISTINCT
A.EMPL_NO
,A.EMP_NAME
FROM EMPLOYEES A
WHERE ( A.EMP_NAME LIKE N'%JAMILA%' )
END-EXEC
This fails:
MOVE N'%JAMILA%' to EMP-NAME
EXEC SQL
DECLARE mycursor CURSOR FOR SELECT DISTINCT
A.EMPL_NO
,A.EMP_NAME
FROM EMPLOYEES A
WHERE ( A.EMP_NAME LIKE :EMP-NAME )
END-EXEC
Why?
Resolution:
The problem is that the space padding in the host variable causes the comparison to be false if the actual column data does not also have those spaces.
You can get around this by padding the data item with "%" characters as follows:
MOVE ALL "%" to EMP-NAME
MOVE "%JAMILA%" TO EMP-NAME(1:8)
You could also define the host variable as a group item using level 49s as follows:
something like:
Then use group name EMP-NAME in WHERE clause.
The precompiler recognizes the 49 levels as a variable length host variable and will limit the size of the field to the value of the length field.
You can get around this by padding the data item with "%" characters as follows:
MOVE ALL "%" to EMP-NAME
MOVE "%JAMILA%" TO EMP-NAME(1:8)
You could also define the host variable as a group item using level 49s as follows:
something like:
01 EMP-NAME.
   49  EMP-NAME-LEN     PIC S9(4) COMP.
   49  EMP-NAME-VALUE   PIC N(50) USAGE NATIONAL.
MOVE "%JAMILA%" TO EMP-NAME-VALUE
MOVE 8 TO EMP-NAME-LEN
Then use group name EMP-NAME in WHERE clause.
The precompiler recognizes the 49 levels as a variable length host variable and will limit the size of the field to the value of the length field.
Old KB# 35448
        
