Null string comparison.
Posted in 1999
Topics: Stored Procedures & SPL
If I have two char(1) variables A and B, I need to do some actions when the two variables differ. I used IF A<>B THEN LET lnCount=lnCount+1 ELSE DISPLAY "Same values" END IF When 1 of the variable is null, I will get "Same values" which is not true. How to overcome this null comparison problem? Thanks.
JouhShenToh@pg.SLR.com wrote: > If I have two char(1) variables A and B, I need to do some actions when the > two variables differ. I used > IF A<>B THEN > LET lnCount=lnCount+1 > ELSE > DISPLAY "Same values" > END IF > > When 1 of the variable is null, I will get "Same values" which is not true. > How to overcome this null comparison problem? > > Thanks. I think you've got to explicitly test for null IF (A<>B) AND A IS NOT NULL AND B IS NOT NULL THEN LET lnCount=lnCount+1 ELSE DISPLAY "Same values" END IF -- ---------------------------------------------------------------------- "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world." -Albert Einstein
<JouhShenToh@pg.SLR.com> schrieb in im Newsbeitrag: 7odcup$eg5$1@news.xmission.com... > > If I have two char(1) variables A and B, I need to do some actions when the > two variables differ. I used > IF A<>B THEN > LET lnCount=lnCount+1 > ELSE > DISPLAY "Same values" > END IF > > When 1 of the variable is null, I will get "Same values" which is not true. > How to overcome this null comparison problem? > > > Thanks. In 4GL every compare will enter the ELSE-part, whenever one of the two values is NULL. So there are two possiblities. IF (A<>B) OR (A IS NULL AND B IS NOT NULL) OR (A IS NOT NULL AND B IS NULL) THEN .... or if You don't want to distinguish between the empty word and NULL, You can use IF (A<>B) OR (LENGTH(A) <> LENGTH(B)) THEN ... Wolfgang