在列中查找长度超过15个字符的单词

时间:2013-07-29 09:52:34

标签: oracle

我有一个名为SIRKET的表和两个名为sirket_unvancust_id的列。我想在sirket_unvan中找到长度超过15个字符的单词。

我用

select sirket_unvan from SIRKET where cust_id=0

但是怎么能找到超过15个字符的单词呢?我想将这些话报告给另一栏。我认为应该写程序。一行可以有2个或更多单词,长度超过15个字符

 cust_id |sirket_unvan
---------+----------------------------------------------
  0      | sdsd Afdfdgfdgdgdg fdgfgfgf fgfgf           
  0      | dfdfds dffd dsfdffggfdgfdgfdgfgdgdfgd fdfdfd
  0      | sdfsdfsdf dsfdsfdsfdsdgfgfgf               
  0      | sdfsdfsd sdfsdfsdf
  1      | sdfsdfsdf dsfdsfdsfdsdgfgfgf 
  2      | dsfdsfs sdfsdfsdgfhfh

我想要这个输出

   cust_id   | sirket_unvan                                |longerthan15characterwords
    ---------+---------------------------------------------+---------------------------
      0      | sdsd Afdfdgfdgdgdg fdgfgfgf fgfgf           | Afdfdgfdgdgdg
      0      | dfdfds dffd dsfdffggfdgfdgfdgfgdgdfgd fdfdfd| dsfdffggfdgfdgfdgfgdgdfgd
      0      | sdfsdfsdf dsfdsfdsfdsdgfgfgf                | dsfdsfdsfdsdgfgfgf

3 个答案:

答案 0 :(得分:3)

如果您想要列中的所有单词,则必须编写SQL函数。否则,正则表达式可以捕获一行中的第一个和最后一个15个字符的单词。

使用SQL函数

CREATE OR REPLACE function wordslongerthan(line IN VARCHAR2, threshold IN NUMBER)
return varchar2 deterministic
is
   first_location INTEGER := 0;
   tmp_line VARCHAR2(4000);
   word VARCHAR2(4000);
begin
   first_location := REGEXP_INSTR(line, '\S{' || to_char(threshold) || '}');
   if first_location = 0 then return null; end if;

   tmp_line := SUBSTR(line, first_location);
   first_location := REGEXP_INSTR(tmp_line, '\s');
   if first_location = 0 then return tmp_line; end if;

   word := SUBSTR(tmp_line, 1, first_location-1);
   tmp_line := SUBSTR(tmp_line, first_location);
   return word || ' ' || wordslongerthan(tmp_line, threshold);
end;
/

SELECT sirket_unvan, cust_id, wordslongerthan(sirket_unvan, 15)
  FROM sirket
 WHERE cust_id = 0 AND regexp_instr(sirket_unvan, '\S{15}') > 0 ;

使用正则表达式:

\S{15,}选择任意15个或更多非空白连续字符。

因此,你可以找到超过15个字符的最后一个单词:

SELECT sirket_unvan,
       cust_id,
       regexp_replace(sirket_unvan, '(.*\s|^)(\S{15,}).*', '\2')
  FROM sirket
 WHERE cust_id = 0
   AND regexp_instr(sirket_unvan, '\S{15}') > 0 ;

capture

第一个捕获组((.*\s|^))匹配任何直到空白字符或字符串开头的内容;第二个捕获组是你想要的(因此反向引用作为第三个参数),其余的与.*匹配,以便它从被替换的表达式中消失。

你可以找到第一个这样的词:

SELECT sirket_unvan,
       cust_id,
       regexp_substr(sirket_unvan, '\S{15,}')
  FROM sirket
 WHERE cust_id = 0
   AND regexp_instr(sirket_unvan, '\S{15}') > 0 ;

capture

答案 1 :(得分:1)

试试这个:

select cust_id , sirket_unvan, sirket_unvan1 from (
SELECT DISTINCT cust_id ,
          SIRKET_UNVAN,
          REGEXP_SUBSTR(SIRKET_UNVAN , '[[:alpha:]]+', 1, LEVEL) SIRKET_UNVAN1
FROM MY_TABLE9
CONNECT BY REGEXP_SUBSTR(SIRKET_UNVAN , '[[:alpha:]]+', 1, LEVEL)  IS NOT NULL)
WHERE LENGTH(SIRKET_UNVAN1) > 15;

答案 2 :(得分:0)

     select sirket_unvan from SIRKET where length(sirket_unvan)>15