我有一个名为SIRKET
的表和两个名为sirket_unvan
和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
答案 0 :(得分:3)
如果您想要列中的所有单词,则必须编写SQL函数。否则,正则表达式可以捕获一行中的第一个和最后一个15个字符的单词。
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 ;
第一个捕获组((.*\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 ;
答案 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