sql server: need to escape [?

后端 未结 3 1980
无人及你
无人及你 2020-12-19 06:11

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

相关标签:
3条回答
  • 2020-12-19 06:22

    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.

    0 讨论(0)
  • 2020-12-19 06:33

    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)

    0 讨论(0)
  • 2020-12-19 06:34

    Here's a little sample code. You need to embed the [ within []:

    SELECT FirstName 
      FROM (SELECT '[Test]' AS FirstName) as t 
     WHERE FirstName LIKE '[[]%'
    
    0 讨论(0)
提交回复
热议问题