SQL Server Like Containing Bracket Characters

SQL Server LIKE containing bracket characters


 ... like '[[]%'

You use [ ] to surround a special character (or range)

See the section "Using Wildcard Characters As Literals" in SQL Server LIKE

Edit, 24 Nov 2011

Note: You don't need to escape the closing bracket...

SQL Server LIKE containing bracket characters(Variable)

Hello @ahmad_lababidi welcome to stackoverflow,

If it is not an ASCII character and generally requires a column of type NVARCHAR and a literal with string N '' (Except when the encoding accepts symbols)

for example, the UNICODE code of your character is 55357, so you can search for this character in your query, as in the following example.

CREATE TABLE #FIND (NAME nvarchar(10));
INSERT INTO #FIND VALUES (N'');

SELECT UNICODE(N'') --> 55357

SELECT *
FROM #FIND
WHERE NAME LIKE '%' + NCHAR(55357) + '%'

How can I escape square brackets in a LIKE clause?


LIKE 'WC[[]R]S123456' 

or

LIKE 'WC\[R]S123456' ESCAPE '\'

Should work.

SQL LIKE - Using square bracket (character range) matching to match an entire word

As a general rule, SQL Server's LIKE patterns are much weaker than regular expressions. For your particular example, you can do:

where col not like '%[^a-c]%'

That is, the column contains no characters that are not a, b, or c.

SQL Server LIKE operator not matching values containing square brackets

You have to escape the left square bracket:

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

SQL Server LIKE with square brackets works directly but not using variable

Make your variable longer so that it can store the whole pattern. You can see what value the variable actually has with PRINT @var, which will show you the closing square bracket has been dropped.

declare @var varchar(10) = 'B[A-Z][A-Z]'
print @var

SQL Server - Sql query like operator with special characters

Try this simple way using regular expression as follows:

SELECT * FROM <YourTable>
WHERE <YourColumn> LIKE '%[!@#$%^&*()-_=+{}\|;'':",./[<>?]%' OR <YourColumn> LIKE '%]%';

Note that ] had to be taken separately so that it doesn't end the regular expression.

SQL Server Like is not working with Square brackets and hyphens

This version works:

WHERE Barcode LIKE '$[C0-S12]%' ESCAPE '$'

I notice that the \ escape doesn't work on Rextester. It should work, but it doesn't.

How to escape square bracket when using LIKE?

When you don't close the square bracket, the result is not specified.

However, the story is different when you close the bracket, i.e.

select *
from table1
where code like N'%[blah%]%'

In this case, it becomes a match for (any) + any of ('b','l','a','h','%') + (any). For SQL Server, you can escape characters using the ESCAPE clause.

select * from table1 where code like N'%\[blah%\]%' escape '\'

SQL Fiddle with examples



Related Topics



Leave a reply



Submit