Click here to Skip to main content
15,888,271 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to create a procedure which takes a input parameter and returns all results from table which have starting character same as input provided. For this i write this query :

alter proc returnmatchingrecords
@inputstring varchar(2)
as
begin
select * from returnrecords where city like '%@inputstring%'
end
. how to correct it? Thanks.
Posted

u can even do it this way
SQL
create procedure getdetails(@inputstring varchar(2))
as
select * from returnrecords where substring(city,1,2)=@inputstring --since the input string is having varchar(2) as the data type i presume that u need to get all the records where the city name has first two letters as the city name.I thought that was your requirement
 
Share this answer
 
done it! here what i have done:
SQL
alter proc returnmatchingrecords
@inputstring varchar(2)
as

begin
select * from returnrecords where city like '%'+ @inputstring+ '%'
end

and its working!
 
Share this answer
 
SQL
Declare @inputstring varchar(3) = '201'
Select * From TableName
Where PATINDEX('%,' + CAST(ColumnName AS VARCHAR(30))+',%', ',' +@inputstring + ',') > 0
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900