adamek339
adamek339

Reputation: 79

SQL Server LIKE operator not matching values containing square brackets

I have a table in my database where I inserted all table names in my database. For example I have table name [test].[TestTable] and when I run this query

select * from Tables where Name like N'%[test].[TestTable]%' 

it doesn't return any value but this query works:

select * from Tables where Name like N'%[TestTable]%'

Can someone explain me why?

Upvotes: 0

Views: 778

Answers (2)

Joel Rodrigo
Joel Rodrigo

Reputation: 26

when u use the operator LIKE and in the condition add [], it has another meaning. LIKE "reads" it as any single character within the specified range ([a-f]) or set ([abcdef]).

try using: like N'%TestTable%'

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272106

You have to escape the left square bracket:

SELECT * FROM Tables WHERE Name LIKE N'%[[]test].[[]TestTable]%' 
-----------------------------------------^        ^
--------------------------------------------------+

Upvotes: 1

Related Questions