SQL Server LIKE containing bracket characters

CJS picture CJS · Sep 7, 2010 · Viewed 53.5k times · Source

Using SQL Server 2008. I have a table with the following column:

sampleData (nvarchar(max))

The value for this column in some of these rows are lists formatted as follows:

["value1","value2","value3"]

I'm trying to write a simple query that will return all rows with lists formatted like this, by just detecting the opening bracket.

SELECT * from sampleTable where sampleData like '[%'

The above query doesn't work because '[' is a special character, and I can't for the life of me figure out how to escape the bracket so my query does what I want.

Thanks for any suggestions!

Answer

gbn picture gbn · Sep 7, 2010
 ... 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...