How to escape a string for use with the LIKE operator in SQL Server?

后端 未结 7 1402
半阙折子戏
半阙折子戏 2020-12-15 07:59

I am looking for something that works in SQL Server similar to the @ symbol in c# which causes a string to be taken as it\'s literal. Eg:

strin         


        
7条回答
  •  春和景丽
    2020-12-15 08:33

    From the docs:

    Syntax

    match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]

    Use the ESCAPE option like so:

    SELECT [Name] 
      FROM [Test] 
     WHERE [Name] LIKE (REPLACE(@searchText, '%', '%%') + '%') ESCAPE '%'
    

提交回复
热议问题