Oracle - 按外观替换字符串
Oracle - replace string by appearance
我这里有一个有趣的问题,如果你能分享你的想法......我改变了一点数据,但结构是一样的
create table TestReplace (Description varchar2(500), ParamValue1 number, ParamValue2 number, ParamValue3 number);
insert into TestReplace (Description) values ('This sentence has no parameteres, and it should be shown like this');
insert into TestReplace (Description, ParamValue1) values ('This sentence has only one parametere, and it should be shown right here {param} with rest of text', 100);
insert into TestReplace (Description, ParamValue1, ParamValue2) values ('This sentence has two parameteres, one here {param} and one here {param}, show full sentence', 100, 200);
insert into TestReplace (Description, ParamValue1, ParamValue2, ParamValue3) values ('This sentence has all parameteres, here {param} and here {param} and there {param}', 100, 200, 300);
COMMIT;
在我的句子中,我出现了一个词 {param}
sometimes or never ... 以及列 ParamValue1
、ParamValue2
、ParamValue3
... 我怎么能将第一次出现的单词 {param}
替换为列 ParamValue1
的值,将第二个单词 {param}
替换为列 ParamValue2
的值,将第三个单词替换为列 ParamValue3
[= 的值21=]
我试过这样的东西...
select CASE WHEN ParamValue1 IS NULL
THEN Description
ELSE
substr(Description, 1, INSTR(Description,'{param}', 1, 1) - 1) || ParamValue1 ||
CASE WHEN ParamValue2 IS NULL
THEN substr(Description, INSTR(Description,'{param}', 1, 1) + 7, LENGTH(Description) - INSTR(Description,'{param}', 1, 1) + 6)
WHEN ParamValue2 IS NOT NULL THEN
substr(Description, INSTR(Description,'{param}', 1, 1) + 7, INSTR(Description,'{param}', 1, 2) + 6 - INSTR(Description,'{param}', 1, 1) + 6) || ParamValue2
END
END
END
from TestReplace
但这对我没有任何帮助,个人认为这在较大的行集上不会很好/很快
那么我怎样才能完成这个文本替换呢?
使用 REGEXP_REPLACE 的嵌套调用:
SELECT REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(DESCRIPTION,
'{param}', PARAMVALUE1, 1, 1),
'{param}', PARAMVALUE2, 1, 1),
'{param}', PARAMVALUE3, 1, 1)
FROM TESTREPLACE
我这里有一个有趣的问题,如果你能分享你的想法......我改变了一点数据,但结构是一样的
create table TestReplace (Description varchar2(500), ParamValue1 number, ParamValue2 number, ParamValue3 number);
insert into TestReplace (Description) values ('This sentence has no parameteres, and it should be shown like this');
insert into TestReplace (Description, ParamValue1) values ('This sentence has only one parametere, and it should be shown right here {param} with rest of text', 100);
insert into TestReplace (Description, ParamValue1, ParamValue2) values ('This sentence has two parameteres, one here {param} and one here {param}, show full sentence', 100, 200);
insert into TestReplace (Description, ParamValue1, ParamValue2, ParamValue3) values ('This sentence has all parameteres, here {param} and here {param} and there {param}', 100, 200, 300);
COMMIT;
在我的句子中,我出现了一个词 {param}
sometimes or never ... 以及列 ParamValue1
、ParamValue2
、ParamValue3
... 我怎么能将第一次出现的单词 {param}
替换为列 ParamValue1
的值,将第二个单词 {param}
替换为列 ParamValue2
的值,将第三个单词替换为列 ParamValue3
[= 的值21=]
我试过这样的东西...
select CASE WHEN ParamValue1 IS NULL
THEN Description
ELSE
substr(Description, 1, INSTR(Description,'{param}', 1, 1) - 1) || ParamValue1 ||
CASE WHEN ParamValue2 IS NULL
THEN substr(Description, INSTR(Description,'{param}', 1, 1) + 7, LENGTH(Description) - INSTR(Description,'{param}', 1, 1) + 6)
WHEN ParamValue2 IS NOT NULL THEN
substr(Description, INSTR(Description,'{param}', 1, 1) + 7, INSTR(Description,'{param}', 1, 2) + 6 - INSTR(Description,'{param}', 1, 1) + 6) || ParamValue2
END
END
END
from TestReplace
但这对我没有任何帮助,个人认为这在较大的行集上不会很好/很快
那么我怎样才能完成这个文本替换呢?
使用 REGEXP_REPLACE 的嵌套调用:
SELECT REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(DESCRIPTION,
'{param}', PARAMVALUE1, 1, 1),
'{param}', PARAMVALUE2, 1, 1),
'{param}', PARAMVALUE3, 1, 1)
FROM TESTREPLACE