420 pts.
 like query not running in pg
hi i am running a query to select records which contains abc_ so i am running :

var1= 'abc_' + '%';

so i am running in rpg as

select name from table where name like :var1;

query is running but not selecting any records ..please help me out



Software/Hardware used:
as400
ASKED: July 11, 2010  7:09 PM
UPDATED: July 21, 2010  1:28 AM

Answer Wiki:
And that would find records in table table in the field or column named name <b>beginning</b> with <b>lower case </b> abc_ and an underscore. Phil
Last Wiki Answer Submitted:  July 12, 2010  11:16 pm  by  philpl1jb   44,180 pts.
All Answer Wiki Contributors:  philpl1jb   44,180 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

var1= ‘abc_’ + ‘%’;
select name from table where name like :var1;

Is column “name” a variable-length field?

Tom

 108,055 pts.

 

Two very basic items — from the SQL Reference:

  • The LIKE predicate searches for strings that have a certain pattern. The pattern is specified by a string in which the underscore and percent sign have special meanings. Trailing blanks in a pattern are a part of the pattern.

Be aware the the “underscore” indicates a single-position wild-card character in the pattern. Any character in that position will match your pattern unless you include an “ESCAPE expression” clause.

Also, if name is not a variable-length field and var1 contains trailing blanks after the “%”, then the blanks can be part of the pattern too. I.e., if var1 contains three blanks after the “%”, then name can match the pattern when it has three trailing blanks.

This can be tricky stuff.

Tom

 108,055 pts.

 

Since there are no responses, I’ll add that one common resolution is:

var1= ‘abc_’ + ‘%%%%%%’;

If var1 is a 6 character variable, the resulting value would be ‘abc_%%’ after truncation. No trailing blanks remain in the pattern, so the pattern doesn’t select values that have a blank at the end — the pattern allows any characters in the 5th and 6th positions.

And an ESCAPE clause might alter the SELECT statement to be:

select name from table where name like :var1 ESCAPE '+';

But that would mean that the pattern would need to be ‘abc+_%%’, and now var1 should be 7 characters long.

Having data values that can contain ‘wild’ characters can make selection tricky. Trying to select fixed-length columns using generic specifications in embedded SQL can be tricky.

Control over data values and use of variable length columns can help a lot.

Tom

 108,055 pts.