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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Proc with variables and wildcards

Status
Not open for further replies.

JefB

Programmer
Dec 29, 2000
56
In SQL2000 I have the following Stored Procedure:

CREATE PROCEDURE [dbo].[DrugLookup]

@hint varchar

AS

SELECT StudyDrug, StudyDrug2, DrugUsage
FROM dbo.DrugName
WHERE DrugUsage like @hint
GO


The [DrugUsage] field has key words describing the various uses of a particular drug as well as alternate names.

How can I set up the SP to use wildcards (“%”) so that @hint can be fed as just a single word (or portion) and return all the records in which that keyword is found?

I have tried:
DrugUsage like “%” & @hint & “%”
DrugUsage like ‘“%” & @hint & “%”’
DrugUsage like ‘“%”’ & @hint & ‘“%”’

as well as several other variations on that theme. Either nothing is returned or ALL the records are returned.

If there is a better way to do this, I am open to suggestion.

JefB
 
I have two recommendation. Declare the thength of @hint and use single quotes around the wild card character '%'.

CREATE PROCEDURE [dbo].[DrugLookup]
@hint varchar(30)
AS

SELECT StudyDrug, StudyDrug2, DrugUsage
FROM dbo.DrugName
WHERE DrugUsage like '%' + @hint + '%'
GO

You can also use the charindex function which may prove easier.

SELECT StudyDrug, StudyDrug2, DrugUsage
FROM dbo.DrugName
WHERE charindex(@hint, DrugUsage)>0 Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
The second option worked perfectly!

Thanks

Jef Block
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top