Got a small question ... this is test data
CREATE TABLE #TestReplace (
Description NVARCHAR(500)
,ParamValue1 INT
,ParamValue2 INT
,ParamValue3 INT
);
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
);
In my sentence I have occurrences of a word {param}
sometimes or never ... and columns ParamValue1
, ParamValue2
, ParamValue3
... How could I replace first occurences of a word {param}
with value of column ParamValue1
, second word {param}
with value of column ParamValue2
and third with value of column ParamValue3
... I am unable to change word {param}
into {param1}
, {param2}
and {param3}
and go with simple replace
So far I managed to replace only first occurance ...
SELECT CASE
WHEN CHARINDEX('{param}', DESCRIPTION) > 0
THEN STUFF(DESCRIPTION, CHARINDEX('{param}', DESCRIPTION), LEN('{param}'), ParamValue1)
ELSE DESCRIPTION
END
FROM #TestReplace
This was managed easily in Oracle (Oracle - replace string by appearance)