【发布时间】:2020-02-11 00:42:38
【问题描述】:
我有一个有趣的问题,如果你能分享你的想法......我改变了一些数据,但结构是一样的
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} ...和列ParamValue1、ParamValue2、ParamValue3 ...我如何将第一次出现的单词{param}替换为ParamValue1 列的值,第二个词 {param} 与列 ParamValue2 的值,第三个与列 ParamValue3 的值
我尝试过这样的事情......
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
但这并没有把我带到任何地方,而且我个人认为这在更大的行集上不会很好/很快
那么我该如何完成这个文本替换呢?
【问题讨论】:
-
参数可以编号吗?即,您可以使用
{param1}、{param2}和{param3},而不是字符串{param}的1 倍或更多倍?如果是这样,你可以试试REPLACE(REPLACE(Description, '{param1}', ParamValue1), '{param2}', ParamValue2) -
我希望它可以这样,事情是在页面的渲染中,我用 javascript 的输入控件替换 {param} ...所以如果我有相同的名称,一个替换放入控制任何需要的地方