Square brackets that function in the expression as expected

I am looking for strings containing this string [management]

Here is an example script:

create table #test (st varchar(200))
insert into #test
values 
('hello'),
('hello management'),
('hello [management]'),
('hello [management] blah'),
('hello [management rev] blah');

select * from #test where st like '%\[management]%' ESCAPE '\'

The above finds what I expect, but why doesn't the following find the same lines?

select * from #test where st like '%[[]management[]]%'
+4
source share
2 answers

Great question! It seems that% [[] text []]% should solve:

NOTHING [Text] NOTHING.

But this is not so. Since], unlike [, does not start a pattern search, it does not need escaping. Use% [[] text]% instead.

See "Using Wildcards as Literals" on the MSDN Like page for more information .

+2
source

, :

DECLARE @t table(x varchar(100))
INSERT @t 
SELECT '[management]'

SELECT * from @t where x like '%[[]management]%'

EDIT:

'[]]', , . '[xy]]' , ('x' 'y') , '[]]' , , .

+4

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


All Articles