I need to escape [ in an sql query for SQL Server
select * from sometable where name like \'[something]\';
I actually am looking for a [ be
Use:
select * from sometable where name like '[[]something[]]';
you may use as well:
select * from sometable where name like '\[something\]' escape '\';
Described in LIKE (Transact-SQL) on MSDN.
Embed the [
in []
declare @T table
(
name varchar(20)
)
insert into @T values
('abc'),
('[abc')
select *
from @T
where name like '[[]a%'
Result:
name
--------------------
[abc
Have a look here at what you can do in the like
expression. LIKE (Transact-SQL)
Here's a little sample code. You need to embed the [
within []
:
SELECT FirstName
FROM (SELECT '[Test]' AS FirstName) as t
WHERE FirstName LIKE '[[]%'