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

后端 未结 7 1381
半阙折子戏
半阙折子戏 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:45

    If you don't want to modify the incoming text, you can use the "LEFT" function to create your own "STARTSWITH":

    SELECT [Name] 
      FROM [Test] 
     WHERE @searchText = LEFT( [Name], LEN( @searchText ) )
    

    (Note that you probably need to do extra work to handle the case of NULLs or empty string.)

    EDIT: Removed the incorrect statement about using "LIKE" to search for "%".

    0 讨论(0)
提交回复
热议问题