0

I have a trigger in Oracle SQL.

CREATE OR REPLACE TRIGGER test
BEFORE INSERT ON SomeTable
FOR EACH ROW
    DECLARE str1 VARCHAR(30);
            str2 VARCHAR(30);
    BEGIN
        -- some code
        IF ( str1 <> str 2 ) THEN
            DBMS_OUTPUT.PUT_LINE( ' if ' );
        ELSE
            DBMS_OUTPUT.PUT_LINE( ' else ' );
        END IF;
    END;

Now, this always goes to the else statement, even when the strings are definitely not equal. I tried to use != instead of <> with the same result. However, it works, in reverse, if I just use

IF ( str1 = str2 ) THEN ... ELSE ... END If;

So what is the right way to test for two strings not being equal to each other (in Oracle)?

2
  • 4
    Is the space in str 2 just a typo? Commented Nov 6, 2011 at 19:46
  • 2
    Is it possible that one of the "str" values is NULL at the time of comparison? Commented Nov 6, 2011 at 19:54

3 Answers 3

5

Can you show us the actual values being used? It is possible that the reason for the above behavior is becuase one of the values is null?

If it is possible for str1 and str2 to have null values, your if's should be like..

IF (str1 is null and str2 is null) then
   <statments depending on whether you want to treat nulls as equal>
else if (
   (str1 is null and str2 is not null) or
   (str2 is null and str1 is not null) or
   (str1 <> str2)) then
  <statements when str1 and str2 are not equal>
else
  <statements when str1 and str2 are equal?
end if;
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I had thought setting the string to '' (empty string) would get rid of the null, but apparently '' is the same as null. Thanks.
(1) You have imbalanced parentheses in your else if branch; (2) you duplicate a str1/str2 criterion in the same.
@pilcrow: I'm pretty sure both of those were just typos so I fixed them. Good call pointing them out though.
FWIW, this construct might be more concisely rendered as checking for nullity, then equality, else inequality: IF (s1 IS NULL AND s2 IS NULL) THEN ... ELSE IF (s1 = s2) THEN ... ELSE ... END IF Fewer conditionals to typo. :)
@RajeshChamarthi Sorry to nitpick, but for those Googling this and finding it, I think <statements when str1 and str2 are equal? should be <statements when str1 and str2 are equal>, though all that would just get replaced by the OP's logic, anyway :)
0

This should determine if one character string exists inside another character string:

IF instr(str1,str2)<>0 THEN

Comments

0

Sometimes it is easier to negate the equality condition. E.g. if not equal(val1, val2);

function equals(
  val1 varchar2,
  val2 varchar2
) return boolean is
begin
  if val1 is null then
    return val2 is null;
  elsif val2 is null then
    return false;
  end if;

  return val1 = val2;
end;

And the in your code you would have:

BEGIN
    -- some code
    IF NOT equals(str1, str2) THEN
        DBMS_OUTPUT.PUT_LINE( ' if ' );
    ELSE
        DBMS_OUTPUT.PUT_LINE( ' else ' );
    END IF;
END;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.