islam anas
islam anas

Reputation: 11

Using 'like' statement with parameters

How would I use the 'LIKE' statement in SQL with parameters? Here's what I have so far:

ALTER proc [dbo].[select_user_by_adress]
   @user_adress nvarchar(max)
as
begin
    select * 
    from dbo.a_user_table 
    where user_adress like '%'+ @user_adress +'%'
end

Upvotes: 0

Views: 74

Answers (1)

toddsonofodin
toddsonofodin

Reputation: 513

Build dynamic SQL and then execute. Note the extra quotes needed as escape characters.

ALTER proc [dbo].[select_user_by_adress]
   @user_adress nvarchar(max)
as
begin

declare @sql varchar(max)
@sql = 'select * from dbo.a_user_table where user_adress like ''%' + @user_adress +'%'''
execute (@sql)

end

Upvotes: 1

Related Questions