Oracle函数以非有序方式比较字符串

时间:2018-03-08 11:50:51

标签: sql oracle oracle11g

我需要一个函数来在两个字符串之间进行比较,而不考虑oracle中的顺序。 即“asd”和“sad”应视为相等。 有类似的功能吗?或者我需要编写自己的函数?

4 个答案:

答案 0 :(得分:7)

这可以通过一个简单的java函数来完成,以按字母顺序对字符串的字符进行排序:

CREATE AND COMPILE JAVA SOURCE NAMED SORTSTRING AS
public class SortString {
  public static String sort( final String value )
  {
    final char[] chars = value.toCharArray();
    java.util.Arrays.sort( chars );
    return new String( chars );
  }
};
/

然后您可以创建一个PL / SQL函数来调用:

CREATE FUNCTION SORTSTRING( in_value IN VARCHAR2 ) RETURN VARCHAR2
AS LANGUAGE JAVA NAME 'SortString.sort( java.lang.String ) return java.lang.String';
/

然后你可以对排序的字符串进行简单的比较:

SELECT CASE
       WHEN SORTSTRING( 'ads' ) = SORTSTRING( 'das' )
       THEN 'Equal'
       ELSE 'Not Equal'
       END
FROM   DUAL;

答案 1 :(得分:5)

不完全是火箭科学,但是有效(至少在简单的情况下)。

它做什么?按字母顺序对每个字符串中的字母进行排序并进行比较。

SQL> with test (col1, col2) as
  2    (select 'asd', 'sad' from dual),
  3  inter as
  4    (select
  5       col1, regexp_substr(col1, '[^.]', 1, level) c1,
  6       col2, regexp_substr(col2, '[^.]', 1, level) c2
  7     from test
  8     connect by level <= greatest(length(col1), length(col2))
  9    ),
 10  agg as
 11    (select listagg(c1, '') within group (order by c1) col1_new,
 12            listagg(c2, '') within group (order by c2) col2_new
 13     from inter
 14    )
 15  select case when col1_new = col2_new then 'Equal'
 16              else 'Different'
 17         end result
 18  From agg;

RESULT
---------
Equal

SQL> with test (col1, col2) as
  2    (select 'asd', 'sadx' from dual),
<snip>    

RESULT
---------
Different

SQL>

答案 2 :(得分:1)

另一种解决方案,使用SUBSTR函数和CONNECT BY循环。

SQL Fiddle

查询1

WITH a
     AS (SELECT ROWNUM rn, a1.*
           FROM (    SELECT SUBSTR ('2asd', LEVEL, 1) s1
                       FROM DUAL
                 CONNECT BY LEVEL <= LENGTH ('2asd')
                   ORDER BY s1) a1),
     b
     AS (SELECT ROWNUM rn, a2.*
           FROM (    SELECT SUBSTR ('asd2', LEVEL, 1) s2
                       FROM DUAL
                 CONNECT BY LEVEL <= LENGTH ('asd2')
                   ORDER BY s2) a2)
SELECT CASE COUNT (NULLIF (s1, s2)) WHEN 0 THEN 'EQUAL' ELSE 'NOT EQUAL' END
          res
  FROM a INNER JOIN b ON a.rn = b.rn

<强> Results

|   RES |
|-------|
| EQUAL |

编辑:用于字母数字字符串的PL / SQL Sort函数。

CREATE OR replace FUNCTION fn_sort(str VARCHAR2) 
  RETURN VARCHAR2 DETERMINISTIC AS 
 v_s VARCHAR2(4000); 
BEGIN 

  SELECT LISTAGG(substr(str, LEVEL, 1), '') 
           within GROUP ( ORDER BY substr(str, LEVEL, 1) ) 
       INTO   v_s 
  FROM   dual 
  CONNECT BY LEVEL < = length(str); 

  RETURN v_s;
END;
/


select  fn_sort('shSdf3213Js') as s
from dual;

|           S |
|-------------|
| 1233JSdfhss |

答案 3 :(得分:0)

如果您想创建自己的排序功能,可以使用以下代码

CREATE OR REPLACE FUNCTION sort_text (p_text_to_sort VARCHAR2) RETURN VARCHAR2
IS
    v_sorted_text VARCHAR2(1000);
BEGIN
    v_sorted_text := p_text_to_sort;
    FOR i IN 1..LENGTH(p_text_to_sort)
    LOOP
        FOR j IN 1..LENGTH(p_text_to_sort)
        LOOP
            IF SUBSTR(v_sorted_text, j, 1)||'' > SUBSTR(v_sorted_text, j+1, 1)||'' THEN
                 v_sorted_text := SUBSTR(v_sorted_text, 1, j-1)||
                                  SUBSTR(v_sorted_text, j+1, 1)||
                                  SUBSTR(v_sorted_text, j, 1)||
                                  SUBSTR(v_sorted_text, j+2);
            END IF;
        END LOOP;
    END LOOP;
    RETURN v_sorted_text;
END;
/

SELECT SORT_TEXT('zlkdsadfsdfasdf') SORTED_TEXT
  FROM dual;

SORTED_TEXT                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
---------------
aaddddfffklsssz