1

To match all records with an nvarchar column that contains substring Type [B]

for example : Owner [C] Type [B] Subtype [X]

the following works: LIKE '%Type [[]B[]]%'

But it's rather confusing syntax.

Is there a better alternative ?

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
BaltoStar
  • 8,165
  • 17
  • 59
  • 91

2 Answers2

3

You can use ESCAPE option.

The query will be LIKE '%Type #[B]%' ESCAPE '#'

Please find the working query with sample data:

DECLARE @TextTable TABLE (String NVARCHAR (500));
INSERT INTO @TextTable(String) SELECT ('Owner [C] Type [B] Subtype [X]');
INSERT INTO @TextTable(String) VALUES ('Owner [C] Type [B Subtype [X]');
INSERT INTO @TextTable(String) VALUES ('Owner [C] Type B Subtype [X]');

SELECT *
FROM @TextTable
WHERE String LIKE '%Type #[B]%' ESCAPE '#'

Working demo on db<>fiddle

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
2

Yes, you can use ESCAPE and define your custom escape character:

select *
from   (select 'Owner [C] Type [B] Subtype [X]' as field) as t
where   
   t.field LIKE '%Type \[B]%' ESCAPE '\' --can be any char
   --t.field LIKE '%Type .[B]%' ESCAPE '.' 
Vanderlei Morais
  • 556
  • 6
  • 11