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)
SQL Server REPLACE() FunctionThe REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.
If you'd like to replace a substring with another string, simply use the REPLACE function. This function takes three arguments: The string to change (which in our case was a column). The substring to replace.
SELECT REPLACE(REPLACE(REPLACE(REPLACE('3*[4+5]/{6-8}', '[', '('), ']', ')'), '{', '('), '}', ')'); We can see that the REPLACE function is nested and it is called multiple times to replace the corresponding string as per the defined positional values within the SQL REPLACE function.
In a view, you could do it like: select case when col1 like '00%' then stuff(col1, 1, 2, '11') else col1 end from YourTable; Live example at SQL Fiddle. Just a note, the substring should be "substring(col1, 3, len(col1)-2)"because you want to start at 3rd character and the characters are numbered from 1, not 0.
You can chain these together using APPLY
:
SELECT COALESCE(v3.DESCRIPTION, v2.DESCRIPTION, v1.DESCRIPTION, tr.DESCRIPTION)
FROM #TestReplace tr CROSS APPLY
(VALUES (CASE WHEN tr.Description LIKE '%{param}%'
THEN STUFF(tr.DESCRIPTION, CHARINDEX('{param}', tr.DESCRIPTION), LEN('{param}'), tr.ParamValue1)
END)
) v1(description) CROSS APPLY
(VALUES (CASE WHEN v1.Description LIKE '%{param}%'
THEN STUFF(v1.DESCRIPTION, CHARINDEX('{param}', v1.DESCRIPTION), LEN('{param}'), tr.ParamValue2)
END)
) v2(description) CROSS APPLY
(VALUES (CASE WHEN v2.Description LIKE '%{param}%'
THEN STUFF(v2.DESCRIPTION, CHARINDEX('{param}', v2.DESCRIPTION), LEN('{param}'), ParamValue3)
END)
) v3(description);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With