示例中的正则表达式将笼子代码的前 24 个字符和 6 个字符放入捕获组 1 和 2。
因此,通过将其替换为\1\2
您可以松开两者之间的字符。
子字符串确保结果不会超过 30,即使没有进行替换。
并且感谢 CASE,如果它不超过 30,那么 output=input。
select input,
(case
when length(input) > 30 then substr(REGEXP_REPLACE(input, '^(.{24}).*(\(\w{4}\))', '\1\2'),1,30)
else input
end) as output
from (
select 'Arrow Industries International-MX7(4432)' as input from dual
union all
select 'Wooden BowIndustries International-WBC' from dual
) q
如果笼码的长度是可变的,那么它就变得有点复杂了。由于我们需要计算要添加到笼子代码中的子字符串的长度:
select input,
(case
when length(input) > 30
then substr(input,1,30-coalesce(length(REGEXP_SUBSTR(input,'\(\w{1,28}\)')),0)) || REGEXP_SUBSTR(input,'\(\w{1,28}\)')
else input
end) as output
from (
select 'Arrow Industries International-MX7(1234567)' as input from dual
union all
select 'Bow Industries International-BII(a12)' from dual
) q
要在更新中使用它:
update manufacture_table
set name=substr(name,1,30-coalesce(length(REGEXP_SUBSTR(name,'\(\w{1,28}\)')),0)) || REGEXP_SUBSTR(name,'\(\w{1,28}\)')
where length(name)>30