Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Like with Parameters 1

Status
Not open for further replies.

ceyhorn

Programmer
Nov 20, 2003
93
0
0
US
I am trying to create a search for a grantee, currently i am just passing it in as a parameter, but the drawback is that it has to be an exact match. How can I use the LIKE in my WHERE clause so that it looks for the words in the database?
Code:
SELECT ApplicationID, GrantCountID, GranteeName, ProjectTitle, FundingBlock, StartDate, EndDate, FundingSourceID, PlanningYear FROM dbo.tblApp WHERE (@GranteeName = GranteeName)

Thanks in advance,
Chris
 
then replace it with...

WHERE @GranteeName LIKE 'GranteeName'

-L
 
What about using the % in front and behind the text search?
 
yes...try this..

WHERE @GranteeName LIKE '%GranteeName%'

it means containing the GranteeName

else

WHERE @GranteeName LIKE '%GranteeName'

it means ending with GranteeName

Else

WHERE @GranteeName LIKE 'GranteeName%'

it means starting with GranteeName

-L
 
Beware of using like with a wildcard as the first character. This usage means that the indexes cannot be used and thus your query will become extremely slow as the number of records grow. Best to insist the users have to give you the first part of the name.

If you cannot, then I would suggest that you use if statements to speed the query for when the user gave you the exact match. Then you can search for the exact match and only if no records are returned, use the slower query.

Questions about posting. See faq183-874
 
Correct SQL would be something like:

Code:
WHERE
   GranteeName LIKE GranteeName + '%'

-------------------------------------
It is better to have honor than a good reputation.
(Reputation is what other people think about you. Honor is what you know about yourself.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top