0

In SQL Server, how to search special characters like square brackets?

I have tried using code below to search .

select * 
from dbo.Content
where title like '%\[TESTING(TEST)(\[]%' escape '\'.

I need to manually put backslash in front of all square bracket in order to get the result. Does anyone know what query/function can use to query all kind of data especially special characters.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nettium
  • 67
  • 2
  • 3
  • 9

2 Answers2

1

To search for [ just write [[] Any special character can be escaped this way. So % will be [%]

Or maybe this suits more your question

REPLACE(REPLACE(REPLACE(@myString, '[', '[[]'), '_', '[_]'), '%', '[%]')

How to escape a string for use with the LIKE operator in SQL Server?

Marek Vitek
  • 1,573
  • 9
  • 20
0

Instead of checking for Special character we can check for alphanumeric and then have NOT (!) to get the row where title has special characters. (WHERE title LIKE '%[^a-z0-9 .]%' )

Rohit Padma
  • 603
  • 5
  • 15