Oracle PL/SQL string compare issue

前端 未结 7 1087
予麋鹿
予麋鹿 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 15:04

    I've created a stored function for this text comparison purpose:

    CREATE OR REPLACE FUNCTION TextCompare(vOperand1 IN VARCHAR2, vOperator IN VARCHAR2, vOperand2 IN VARCHAR2) RETURN NUMBER DETERMINISTIC AS
    BEGIN
      IF vOperator = '=' THEN
        RETURN CASE WHEN vOperand1 = vOperand2 OR vOperand1 IS NULL AND vOperand2 IS NULL THEN 1 ELSE 0 END;
      ELSIF vOperator = '<>' THEN
        RETURN CASE WHEN vOperand1 <> vOperand2 OR (vOperand1 IS NULL) <> (vOperand2 IS NULL) THEN 1 ELSE 0 END;
      ELSIF vOperator = '<=' THEN
        RETURN CASE WHEN vOperand1 <= vOperand2 OR vOperand1 IS NULL THEN 1 ELSE 0 END;
      ELSIF vOperator = '>=' THEN
        RETURN CASE WHEN vOperand1 >= vOperand2 OR vOperand2 IS NULL THEN 1 ELSE 0 END;
      ELSIF vOperator = '<' THEN
        RETURN CASE WHEN vOperand1 < vOperand2 OR vOperand1 IS NULL AND vOperand2 IS NOT NULL THEN 1 ELSE 0 END;
      ELSIF vOperator = '>' THEN
        RETURN CASE WHEN vOperand1 > vOperand2 OR vOperand1 IS NOT NULL AND vOperand2 IS NULL THEN 1 ELSE 0 END;
      ELSIF vOperator = 'LIKE' THEN
        RETURN CASE WHEN vOperand1 LIKE vOperand2 OR vOperand1 IS NULL AND vOperand2 IS NULL THEN 1 ELSE 0 END;
      ELSIF vOperator = 'NOT LIKE' THEN
        RETURN CASE WHEN vOperand1 NOT LIKE vOperand2 OR (vOperand1 IS NULL) <> (vOperand2 IS NULL) THEN 1 ELSE 0 END;
      ELSE
        RAISE VALUE_ERROR;
      END IF;
    END;
    

    In example:

    SELECT * FROM MyTable WHERE TextCompare(MyTable.a, '>=', MyTable.b) = 1;
    
    0 讨论(0)
提交回复
热议问题