2

我有一个名为的表SIRKET和两个名为sirket_unvanand的列cust_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
4

3 回答 3

3

If you want all words in your column, you will have to write an SQL function. Otherwise, regular expressions can catch the first and last 15-character words in a line.

With an SQL function

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 ;

With regular expressions:

\S{15,} selects any 15 or more non-whitespace consecutive characters.

Therefore you can find the last word longer than 15 characters like this:

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

The first capture group ((.*\s|^)) matches anything up to a whitespace character, or at start of string; the second capture group is what you want (hence the backreference as third argument), and the rest is matched with .* so that it disappears from the replaced expression.

And you can find the first word like this:

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

于 2013-07-29T11:36:44.967 回答
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;
于 2013-07-30T02:55:33.727 回答
0
     select sirket_unvan from SIRKET where length(sirket_unvan)>15
于 2013-07-29T09:57:38.887 回答