Wildcard SQL Replace Command

Basically, I am trying to replace everything in a line in brackets.

So, for example, the string '123 [1abc] abc' will become '123 [xx] abc'

select replace(string,'[%]','[xx]') as string2 from table1

Except that it won't work.

The values ​​in parentheses are always different, and it is simply impossible to find all the individual possibilities. In addition, some of the values ​​in brackets also appear outside the brackets, but I want them to be changed for the part inside.

I work in Microsoft SQL Server, if that matters.

+4
source share
2 answers

Assuming that there is only one such expression, and the square brackets appear only once, you can use stuff()to build the line:

select stuff(str,
             charindex('[', str) + 1,
             charindex(']', str) - charindex('[', str) - 1,
             'xx')
+2

. , "xx", , .

SELECT LEFT(string, CHARINDEX('[', string)) 
       + 'xx' 
       + RIGHT(string, LEN(string) - CHARINDEX(']', string) + 1) AS string2
    FROM table1;
0

Source: https://habr.com/ru/post/1589273/


All Articles