开发者

Difference between two unordered delimited strings in PL/SQL

开发者 https://www.devze.com 2023-04-03 15:07 出处:网络
Is there a simple elegant method to return the difference between two unordered, delimited strings in Oracle using PL/SQL?

Is there a simple elegant method to return the difference between two unordered, delimited strings in Oracle using PL/SQL?

Example:

String A: "a1, b4, g3, h6, t8, a0"
String B: "b4, h6, a0, t8, a1"

Difference: "g3"

I don't have access to APEX_UTIL as suggested in this answer Difference between tw开发者_JS百科o unordered deliminted lists (Oracle)


Not sure about elegant but this would work:

WITH t1 AS (SELECT 'a1, b4, g3, h6, t8, a0' str FROM dual),
     t2 AS (SELECT 'b4, h6, a0, t8, a1' str     FROM dual)
--
SELECT val
  FROM t1, 
       xmltable('/root/e/text()'
       passing xmltype('<root><e>' || 
                       replace(t1.str,', ','</e><e>') || 
                       '</e></root>')
       columns val varchar2(10) path '/')
MINUS
SELECT val
  FROM t2, 
       xmltable('/root/e/text()'
       passing xmltype('<root><e>' || 
                       replace(t2.str,', ','</e><e>') || 
                       '</e></root>')
       columns val varchar2(10) path '/')

hope this helps


I usually prefer using the MULTISET operator for situations like this.

The solution below is not exactly elegant though as you have to tokenize the string to use the MULTISET operator but if you get the lists as collections then this would be very easy (or if you have a common tokenizer already). (The tokenizer below is not very fast.)

DECLARE
   TYPE VARCHARTABLE IS TABLE OF VARCHAR2(2000);

   A VARCHAR2(32767) := 'a1, b4, g3, h6, t8, a0';
   B VARCHAR2(32767) := 'b4, h6, a0, t8, a1';

   onlyInA VARCHARTABLE;
   onlyInB VARCHARTABLE;

   FUNCTION tokenize( v IN VARCHAR2 )
   RETURN VARCHARTABLE 
   IS
      mReturn VARCHARTABLE := VARCHARTABLE();
      mTemp   VARCHAR2(2000);
      mChar   VARCHAR2(1);
      mIdx    INTEGER := 1;

      PROCEDURE appendToken( token IN VARCHAR2 )
      IS
      BEGIN
         IF TRIM(token) IS NOT NULL THEN
            mReturn.EXTEND(1);
            mReturn( mReturn.LAST ) := TRIM(token);
         END IF;
      END appendToken;
   BEGIN
      LOOP
         mChar := SUBSTR( v, mIdx, 1);

         IF mChar = ',' THEN
               appendToken( mTemp );
               mTemp := NULL;
         ELSIF mChar IS NULL THEN 
            appendToken( mTemp );
            EXIT;
         ELSE
            mTemp := mTemp || mChar;
         END IF;

         mIdx := mIdx + 1;
      END LOOP;

      RETURN mReturn;
  END tokenize;

  FUNCTION toVarchar( v IN VARCHARTABLE )
  RETURN VARCHAR2
  IS
     mReturn VARCHAR2(32767);
     mIdx    INTEGER := 0;
  BEGIN
     mIdx := v.FIRST;

     WHILE mIdx IS NOT NULL LOOP
        IF mReturn IS NOT NULL THEN
           mReturn := mReturn || ',';
        END IF;
        mReturn := mReturn || v(mIdx);

        mIdx := v.NEXT(mIdx);
     END LOOP;

     RETURN mReturn;
  END toVarchar;
BEGIN
   onlyInA := tokenize(A) MULTISET EXCEPT tokenize(B);
   onlyInB := tokenize(B) MULTISET EXCEPT tokenize(A);

   DBMS_OUTPUT.put_line( 'Only in A : ' || toVarchar(onlyInA) );
   DBMS_OUTPUT.put_line( 'Only in B : ' || toVarchar(onlyInB) );
END;


If you know they are going to be comma and / or space delimited then this would work and is a lot simpler.

create or replace function compare_strings ( PString1 char, Pstring2 char ) return char is 

   v_string1 varchar2(100) := replace(replace(Pstring1,',',''),' ','');
   v_string2 varchar2(100) := replace(replace(Pstring2,',',''),' ','');

begin

   if replace(translate( v_string1, v_string2, ' '), ' ', '') is null then
      return replace(translate( v_string2, v_string1, ' '), ' ', '') ;
   else
      return replace(translate( v_string1, v_string2, ' '), ' ', '');
   end if;

end;

EDIT: change to return string.

0

精彩评论

暂无评论...
验证码 换一张
取 消