从 Oracle Sql 中出现在特定单词之前的句子中获取一组单词(字符和非字符,由 space 分隔)
Fetch a set of words(char and non char seperated by space) from a sentence appearing prior to a particular words in Oracle Sql
我想拆分一个句子并在 Oracle SqL.
中的特定单词之前获取由 space 分隔的单词子集
我尝试了 regexp substring, instring 但无法找到解决方案。
源句:
收取包裹项目 25% 的佣金
借记项目的预扣税为 27.5%
对提供的服务收取 30.09876% 的调查费用。
条件:
获取单词 'on'
首次出现之前的所有单词
预期输出:
佣金为 25%
预扣税为 27.5%
30.09876% 的调查费用
您可以使用简单的 SUBSTR 和 REGEXP_INSTR
查询
WITH
sentences (sentence)
AS
(SELECT 'Being 25 % commission on package item charged' FROM DUAL
UNION ALL
SELECT 'Being 27.5 % Withholding tax on items debited' FROM DUAL
UNION ALL
SELECT 'Being 30.09876 % survey fees on services delivered' FROM DUAL
UNION ALL
SELECT 'Alice has a hat with flowers on, it is pretty.' FROM DUAL)
SELECT sentence,
SUBSTR (sentence, 1, REGEXP_INSTR (sentence, '\Won\W') - 1) AS short_sentence
FROM sentences;
结果
SENTENCE SHORT_SENTENCE
_____________________________________________________ _______________________________
Being 25 % commission on package item charged Being 25 % commission
Being 27.5 % Withholding tax on items debited Being 27.5 % Withholding tax
Being 30.09876 % survey fees on services delivered Being 30.09876 % survey fees
Alice has a hat with flowers on, it is pretty. Alice has a hat with flowers
您可以使用正则表达式 ^(.*?[^[:alpha:]])??(on[^[:alpha:]].*)?$
,如果匹配,则将值替换为第一个捕获组的内容:
SELECT value,
REGEXP_REPLACE(
value,
'^(.*?[^[:alpha:]])??(on[^[:alpha:]].*)?$',
'',
1,
1,
'i'
) AS value_until_first_on
FROM table_name
其中,对于示例数据:
CREATE TABLE table_name ( value ) AS
SELECT 'Being 25 % commission on package item charged' FROM DUAL UNION ALL
SELECT 'Being 27.5 % Withholding tax on items debited' FROM DUAL UNION ALL
SELECT 'Being 30.09876 % survey fees on services delivered.' FROM DUAL UNION ALL
SELECT 'Alice has a hat with flowers on, it is pretty.' FROM DUAL UNION ALL
SELECT 'On Tuesday, it was hot.' FROM DUAL UNION ALL
SELECT 'This sentence ends with on' FROM DUAL UNION ALL
SELECT 'One Two Three Four' FROM DUAL UNION ALL
SELECT 'Four Three Two One Zero' FROM DUAL UNION ALL
SELECT 'We went, on Tuesday, on the train.' FROM DUAL;
输出:
VALUE | VALUE_UNTIL_FIRST_ON
:-------------------------------------------------- | :----------------------------
Being 25 % commission on package item charged | Being 25 % commission
Being 27.5 % Withholding tax on items debited | Being 27.5 % Withholding tax
Being 30.09876 % survey fees on services delivered. | Being 30.09876 % survey fees
Alice has a hat with flowers on, it is pretty. | Alice has a hat with flowers
On Tuesday, it was hot. | null
This sentence ends with on | This sentence ends with on
One Two Three Four | One Two Three Four
Four Three Two One Zero | Four Three Two One Zero
We went, on Tuesday, on the train. | We went,
db<>fiddle here
我想拆分一个句子并在 Oracle SqL.
中的特定单词之前获取由 space 分隔的单词子集我尝试了 regexp substring, instring 但无法找到解决方案。
源句: 收取包裹项目 25% 的佣金
借记项目的预扣税为 27.5%
对提供的服务收取 30.09876% 的调查费用。
条件: 获取单词 'on'
首次出现之前的所有单词预期输出: 佣金为 25%
预扣税为 27.5%
30.09876% 的调查费用
您可以使用简单的 SUBSTR 和 REGEXP_INSTR
查询
WITH
sentences (sentence)
AS
(SELECT 'Being 25 % commission on package item charged' FROM DUAL
UNION ALL
SELECT 'Being 27.5 % Withholding tax on items debited' FROM DUAL
UNION ALL
SELECT 'Being 30.09876 % survey fees on services delivered' FROM DUAL
UNION ALL
SELECT 'Alice has a hat with flowers on, it is pretty.' FROM DUAL)
SELECT sentence,
SUBSTR (sentence, 1, REGEXP_INSTR (sentence, '\Won\W') - 1) AS short_sentence
FROM sentences;
结果
SENTENCE SHORT_SENTENCE
_____________________________________________________ _______________________________
Being 25 % commission on package item charged Being 25 % commission
Being 27.5 % Withholding tax on items debited Being 27.5 % Withholding tax
Being 30.09876 % survey fees on services delivered Being 30.09876 % survey fees
Alice has a hat with flowers on, it is pretty. Alice has a hat with flowers
您可以使用正则表达式 ^(.*?[^[:alpha:]])??(on[^[:alpha:]].*)?$
,如果匹配,则将值替换为第一个捕获组的内容:
SELECT value,
REGEXP_REPLACE(
value,
'^(.*?[^[:alpha:]])??(on[^[:alpha:]].*)?$',
'',
1,
1,
'i'
) AS value_until_first_on
FROM table_name
其中,对于示例数据:
CREATE TABLE table_name ( value ) AS
SELECT 'Being 25 % commission on package item charged' FROM DUAL UNION ALL
SELECT 'Being 27.5 % Withholding tax on items debited' FROM DUAL UNION ALL
SELECT 'Being 30.09876 % survey fees on services delivered.' FROM DUAL UNION ALL
SELECT 'Alice has a hat with flowers on, it is pretty.' FROM DUAL UNION ALL
SELECT 'On Tuesday, it was hot.' FROM DUAL UNION ALL
SELECT 'This sentence ends with on' FROM DUAL UNION ALL
SELECT 'One Two Three Four' FROM DUAL UNION ALL
SELECT 'Four Three Two One Zero' FROM DUAL UNION ALL
SELECT 'We went, on Tuesday, on the train.' FROM DUAL;
输出:
VALUE | VALUE_UNTIL_FIRST_ON :-------------------------------------------------- | :---------------------------- Being 25 % commission on package item charged | Being 25 % commission Being 27.5 % Withholding tax on items debited | Being 27.5 % Withholding tax Being 30.09876 % survey fees on services delivered. | Being 30.09876 % survey fees Alice has a hat with flowers on, it is pretty. | Alice has a hat with flowers On Tuesday, it was hot. | null This sentence ends with on | This sentence ends with on One Two Three Four | One Two Three Four Four Three Two One Zero | Four Three Two One Zero We went, on Tuesday, on the train. | We went,
db<>fiddle here