Oracle SQL - 字符串不等于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8029828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle SQL - string not equal to
提问by pkr298
I have a trigger in Oracle SQL.
我在 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
现在,这总是转到 else 语句,即使字符串绝对不相等。我尝试使用 != 而不是 <> 具有相同的结果。但是,如果我只是使用它,它会反过来工作
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)?
那么测试两个不相等的字符串的正确方法是什么(在 Oracle 中)?
回答by Rajesh Chamarthi
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..
如果 str1 和 str2 可能具有空值,则您的 if 应该像..
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;
回答by Shyam D
This should determine if one character string exists inside another character string:
这应该确定一个字符串是否存在于另一个字符串中:
IF instr(str1,str2)<>0 THEN
回答by Pawel Solarski
Sometimes it is easier to negate the equality condition. E.g. if not equal(val1, val2);
有时,否定相等条件更容易。例如,如果不等于(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;