oracle查询某个字符之前的全部字符串,在包含它自己的oracle的字符串之前获取所有内容(get everything before a string including itself oracl...

The solution will have to work when the "token" ( the '81' in your example ) appears between two slashes, or right at the beginning of the string and before a slash, or right after the last slash at the end of the string. It should not match if '81' appears as part of a "token" (between slashes or before the first or after the last slash). Also, if the "token" appears more than once, it should be replaced (with everything before it) only once, and if it doesn't appear at all, then the original string should be unchanged.

If these are the rules, then you can do something like I show below. If any of the rules are different, the solution can be modified to accommodate.

I created a few input strings to test all these cases in a WHERE clause. I also created the "search token" and the "replacement text" in a second subquery in the WITH clause. The entire WITH clause should be replaced - it is not part of the solution, it is only for my own testing. In the main query you should use your actual table and column names (and/or hardcoded text).

I use REGEXP_REPLACE to find the token and replace it and everything that comes before it (but not the slash after it, if there is one) with the replacement text. I must be careful with that slash after the search token; I use a backreference in the replacement string in REGEXP_REPLACE for that purpose.

with

event_dynamic_attribute ( attribute_value ) as (

select '28/29/81/732536/1496071' from dual union all

select '29/33/530813/340042/88' from dual union all

select '81/6883/3902/81/993' from dual union all

select '123/45/6789/81' from dual

),

substitution ( token, replacement ) as (

select '81', 'mathguy is great' from dual

)

select attribute_value,

regexp_replace (attribute_value, '(^|.*?/)' || token || '(/|$)',

replacement || '\2', 1, 1) new_attrib_value

from event_dynamic_attribute cross join substitution

;

ATTRIBUTE_VALUE NEW_ATTRIB_VALUE

----------------------- ----------------------------------------

28/29/81/732536/1496071 mathguy is great/732536/1496071

29/33/530813/340042/88 29/33/530813/340042/88

81/6883/3902/81/993 mathguy is great/6883/3902/81/993

123/45/6789/81 mathguy is great

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值