5

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)

3
  • 1
    SQL Server doesn't natively support REGEX, and thus doesn't support REGEX replacement. If you do want something like this, you'll likely want to look at a CLR function.
    – Thom A
    Commented Feb 11, 2020 at 11:28
  • What is your SQL Server version?
    – Zhorov
    Commented Feb 11, 2020 at 11:37
  • Currently on Microsoft SQL Server 2012, but able to update if needed
    – Veljko89
    Commented Feb 11, 2020 at 11:38

1 Answer 1

6

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);
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.