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
How to Create a Copy of an Oracle Table Without Copying the Data
Select Data from Date Range Between Two Dates
Dynamic Sorting Within SQL Stored Procedures
Add Unique Constraint to Combination of Two Columns
Transfer Data from One Database to Another Database
How to Return Multiple Rows from the Stored Procedure? (Oracle Pl/Sql)
Select for Update with SQL Server
How to Use SQL Order by Statement to Sort Results Case Insensitive
How to Insert Data into Two Tables Simultaneously in SQL Server
SQL Server Select to JSON Function
Polymorphism in SQL Database Tables
Differencebetween Function and Procedure in Pl/Sql
How to Pass Parameters to Query in SQL (Excel)
Insert Command :: Error: Column "Value" Does Not Exist
How to Design a Database for User Defined Fields
Show a One to Many Relationship as 2 Columns - 1 Unique Row (Id & Comma Separated List)