比较oracle中的列字符串值

时间:2012-12-07 21:36:15

标签: sql oracle oracle10g

我有表格,我需要比较两列中的值。

Col1      Col2        Col3
------------------------------------
1         sssXYZ2121  XYZ   

现在匹配可能不是单词匹配的完美单词。

E.g。一场比赛

Col2    Col3
-----   --------------------
XYZ     XYZ                  (word to word match)
XYZ     xyz                  (can be case insensitive)
XYZ     gxyzf                (need to search within a string )
XYZ     xyzjjjjjjjjj         (need to search within a string )
XYZ     gggggXYZ             (need to search within a string )
XYZ     Xavier Yellow Zebra  (Match the full form of the abbreviation)

现在我需要找到Col3中的值与Col2不匹配的所有行。 我正在使用Oracle 10g。

3 个答案:

答案 0 :(得分:1)

XYZ,XYZ(单词匹配)

col2 = col3

XYZ,xyz(可以不区分大小写)

upper(col2) = upper(col3)

XYZ,gxyzf(需要在字符串中搜索)

upper(col2) like '%'||upper(col3)||'%'

XYZ,xyzjjjjjjjjj(需要在字符串中搜索)

upper(col2) like upper(col3)||'%'

XYZ,gggggXYZ(需要在字符串中搜索)

upper(col2) like '%'||upper(col3)

XYZ,Xavier Yellow Zebra(匹配缩写的完整形式)

在另一张表中查找

答案 1 :(得分:0)

也许这样的事情对你有用。 where子句有两个条件,只需要满足其中一个条件。当在Col2中找到Col3值时,满足第一个条件,忽略大小写。当Col2是Col3的扩展版本时,第二个条件得到满足。

SELECT Col1, Col2, Col3 FROM Tbl1
LEFT JOIN Abbr ON Col3 = Abbr.Key
WHERE INSTR(UPPER(Col2), UPPER(Col3)) > 0 OR Col2 = Abbr.ExpandedName

如果需要,可以通过遵循第一个条件的工作原理来更改第二个条件,以允许Col2包含扩展名称而不是扩展名称。

答案 2 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE ABBREVIATIONS(
  Short VARCHAR2(10)   CONSTRAINT ABBR__S__PK PRIMARY KEY
                       CONSTRAINT ARRB__S__CHK CHECK( Short = UPPER( Short ) ),
  Value  VARCHAR2(100) CONSTRAINT ABBR__V__U UNIQUE
                       CONSTRAINT ABBR__V__CHK CHECK( Value IS NOT NULL AND Value = UPPER( Value ) )
);

INSERT INTO Abbreviations VALUES ( 'XYZ', 'XAVIER YELLOW ZEBRA' );

CREATE TABLE Tests ( Col1, Col2, Col3 ) AS
          SELECT 1, 'XYZ', 'XYZ' FROM DUAL
UNION ALL SELECT 2, 'xyz', 'XYZ' FROM DUAL
UNION ALL SELECT 3, 'XYZ', 'xyz' FROM DUAL
UNION ALL SELECT 4, 'xyz', 'xyz' FROM DUAL
UNION ALL SELECT 5, 'xyz', 'abcdXYZ' FROM DUAL
UNION ALL SELECT 6, 'xyz', 'XYZefg' FROM DUAL
UNION ALL SELECT 7, 'xyz', 'ghiXYZjkl' FROM DUAL
UNION ALL SELECT 8, 'xyz', 'XaViEr YelloW ZeBrAXXYYZZ' FROM DUAL
UNION ALL SELECT 9, 'Xavier Yellow Zebra', 'XXYZZ' FROM DUAL
UNION ALL SELECT 10, 'xyz', 'xy -- Not a match -- z' FROM DUAL;

查询1

SELECT *
FROM   Tests t
WHERE  UPPER( Col3 ) LIKE '%' || UPPER( Col2 ) || '%'
OR     EXISTS (
  SELECT 'X'
  FROM   ABBREVIATIONS a
  WHERE  (   UPPER( t.col2 ) = a.Short
          OR UPPER( t.Col2 ) = a.Value )
  AND    REPLACE( UPPER( Col3 ), a.Short, a.Value ) LIKE '%' || a.Value || '%'
)

<强> Results

| COL1 |                COL2 |                      COL3 |
|------|---------------------|---------------------------|
|    1 |                 XYZ |                       XYZ |
|    2 |                 xyz |                       XYZ |
|    3 |                 XYZ |                       xyz |
|    4 |                 xyz |                       xyz |
|    5 |                 xyz |                   abcdXYZ |
|    6 |                 xyz |                    XYZefg |
|    7 |                 xyz |                 ghiXYZjkl |
|    8 |                 xyz | XaViEr YelloW ZeBrAXXYYZZ |
|    9 | Xavier Yellow Zebra |                     XXYZZ |