Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - replace string by appearance

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)

like image 834
Veljko89 Avatar asked Feb 11 '20 11:02

Veljko89


People also ask

How do I replace a specific string in SQL?

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.

How do I replace a string in a column in SQL Server?

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.

How do I replace multiple characters in a string in SQL?

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.

How do I change the first 3 characters in SQL?

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.


1 Answers

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);
like image 146
Gordon Linoff Avatar answered Oct 11 '22 19:10

Gordon Linoff