Skip to content
Advertisement

Sql where statement with = works but LIKE does not

Sql where statement with = works but LIKE does not Is there a fix for this?

Here is the exact code:

create table zSyn (xField nvarchar(255));
insert into zSyn(xField)
select 'DEVCON 5 Minute Epoxy amber [1:1]';

--Works and returns 1 row:
select * from zSyn
where xField = 'DEVCON 5 Minute Epoxy amber [1:1]';

--Does NOT return any rows:
select * from zSyn
where xField like '%' + 'DEVCON 5 Minute Epoxy amber [1:1]' + '%'

Advertisement

Answer

You need to escape []:

select * from zSyn
where xField like ('%' + 'DEVCON 5 Minute Epoxy amber ![1:1!]' + '%') ESCAPE '!';

db<>fiddle demo

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement