是否可以将 REPLACE 与 LIKE 结合使用以替换 oracle 数据库列中的多个值
Is it possible to combine REPLACE with LIKE to replace multiple values in oracle database column
这与问题 here 类似,但我不想替换单个值,而是想用匹配的模式替换多个值。
--create table
create table my_table (column1 varchar2(10));
--load table
insert into my_table values ('Test1');
insert into my_table values ('Test2');
insert into my_table values ('Test3');
insert into my_table values ('Test4');
insert into my_table values ('Test5');
insert into my_table values ('Lesson');
--this query replaces 'Test1' with blank
select replace(column1, 'Test1', ' ') from my_table;
--now i want to replace all matching values with blank but i get an error
select replace(column1, like '%Test%', ' ') from my_table; --this throws below error.
--ORA-00936: missing expression
--00936. 00000 - "missing expression"
--*Cause:
--*Action:
--Error at Line: 19 Column: 25
运行 Oracle Database 11g 企业版 Release 11.2.0.1.0
我会用 regexp_replace.
select regexp_replace(column1,'Test[[:digit:]]', ' ') from my_table;
在原来的 post 中,您通过 %Test% 表示您想要用 space 替换整个字符串,如果其中有字符串 "Test":
with my_table(col1) as
( select 'Test1' from dual
union
select 'Test2' from dual
union
select 'thisisaTestofpatternmatching4' from dual
union
select 'thisisa Test ofpatternmatching5' from dual
union
select 'Test at the start' from dual
union
select 'Testat the start no following space' from dual
union
select 'Ending with Test' from dual
union
select 'Ending nospacebeforewithTest' from dual
union
select 'Testy' from dual
union
select 'Lesson' from dual
)
select regexp_replace(col1, '^.*Test.*$', ' ') from my_table;
我怀疑您真的只是想替换“测试”一词吗?它可以在一行中出现多次吗?
select regexp_replace(col1, 'Test', ' ') from my_table;
单词 test 后跟一个数字?
select regexp_replace(col1, 'Test\d', ' ') from my_table;
提示:确保您的测试用例设置为包含测试数据的各种组合,即使它们可能出乎意料。测试正则表达式时,有时您可能会得到意想不到的结果,因此请确保测试所有可能的条件。
这与问题 here 类似,但我不想替换单个值,而是想用匹配的模式替换多个值。
--create table
create table my_table (column1 varchar2(10));
--load table
insert into my_table values ('Test1');
insert into my_table values ('Test2');
insert into my_table values ('Test3');
insert into my_table values ('Test4');
insert into my_table values ('Test5');
insert into my_table values ('Lesson');
--this query replaces 'Test1' with blank
select replace(column1, 'Test1', ' ') from my_table;
--now i want to replace all matching values with blank but i get an error
select replace(column1, like '%Test%', ' ') from my_table; --this throws below error.
--ORA-00936: missing expression
--00936. 00000 - "missing expression"
--*Cause:
--*Action:
--Error at Line: 19 Column: 25
运行 Oracle Database 11g 企业版 Release 11.2.0.1.0
我会用 regexp_replace.
select regexp_replace(column1,'Test[[:digit:]]', ' ') from my_table;
在原来的 post 中,您通过 %Test% 表示您想要用 space 替换整个字符串,如果其中有字符串 "Test":
with my_table(col1) as
( select 'Test1' from dual
union
select 'Test2' from dual
union
select 'thisisaTestofpatternmatching4' from dual
union
select 'thisisa Test ofpatternmatching5' from dual
union
select 'Test at the start' from dual
union
select 'Testat the start no following space' from dual
union
select 'Ending with Test' from dual
union
select 'Ending nospacebeforewithTest' from dual
union
select 'Testy' from dual
union
select 'Lesson' from dual
)
select regexp_replace(col1, '^.*Test.*$', ' ') from my_table;
我怀疑您真的只是想替换“测试”一词吗?它可以在一行中出现多次吗?
select regexp_replace(col1, 'Test', ' ') from my_table;
单词 test 后跟一个数字?
select regexp_replace(col1, 'Test\d', ' ') from my_table;
提示:确保您的测试用例设置为包含测试数据的各种组合,即使它们可能出乎意料。测试正则表达式时,有时您可能会得到意想不到的结果,因此请确保测试所有可能的条件。