Oracle PL/SQL string compare issue

前端 未结 7 1124
予麋鹿
予麋鹿 2020-12-24 14:16

I have the following Oracle PL/SQL codes that may be rusty from you guys perspective:

 DECLARE
 str1  varchar2(4000);
 str2  varchar2(4000);
 BEGIN
   str1:         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-24 14:43

    I compare strings using = and not <>. I've found out that in this context = seems to work in more reasonable fashion than <>. I have specified that two empty (or NULL) strings are equal. The real implementation returns PL/SQL boolean, but here I changed that to pls_integer (0 is false and 1 is true) to be able easily demonstrate the function.

    create or replace function is_equal(a in varchar2, b in varchar2)
    return pls_integer as
    begin
      if a is null and b is null then
        return 1;
      end if;
    
      if a = b then
        return 1;
      end if;
    
      return 0;
    end;
    /
    show errors
    
    begin
      /* Prints 0 */
      dbms_output.put_line(is_equal('AAA', 'BBB'));
      dbms_output.put_line(is_equal('AAA', null));
      dbms_output.put_line(is_equal(null, 'BBB'));
      dbms_output.put_line(is_equal('AAA', ''));
      dbms_output.put_line(is_equal('', 'BBB'));
    
      /* Prints 1 */
      dbms_output.put_line(is_equal(null, null));
      dbms_output.put_line(is_equal(null, ''));
      dbms_output.put_line(is_equal('', ''));
      dbms_output.put_line(is_equal('AAA', 'AAA'));
    end;
    /
    

提交回复
热议问题