Using variable in SQL LIKE statement

前端 未结 9 1341
夕颜
夕颜 2020-12-05 08:52

I\'ve got a sproc (MSSQL 2k5) that will take a variable for a LIKE claus like so:

DECLARE @SearchLetter2 char(1)
SET @SearchLetter = \'t\'
SET @SearchLetter2         


        
9条回答
  •  隐瞒了意图╮
    2020-12-05 09:43

    This works for me on the Northwind sample DB, note that SearchLetter has 2 characters to it and SearchLetter also has to be declared for this to run:

    declare @SearchLetter2 char(2)
    declare @SearchLetter char(1)
    Set @SearchLetter = 'A'
    Set @SearchLetter2 = @SearchLetter+'%'
    select * from Customers where ContactName like @SearchLetter2 and Region='WY'
    

提交回复
热议问题