troubleshooting Question

SQL - Select like statement with variables

Avatar of robthomas09
robthomas09 asked on
Microsoft SQL Server 2008SQL
6 Comments4 Solutions690 ViewsLast Modified:
Hello experts,

I have a sql script here, that has a like portion of my where clause that is not behaving like I wish.  Here it is:

DECLARE @MEDNAME varchar(500)
DECLARE @MED char(200)
DECLARE @Found int
SET @MED= 'Actoplus MET'

             select top 1 medication_name from patient_medication-- WITH (NOLOCK)
         WHERE enterprise_id = '00001'
         AND practice_id = '0001'
         AND person_id = 'C0B99DCB-8080-471C-BE3F-79B8EB02EBF7'
         and medication_name like '%' + @MED + '%' -- THIS LINE IN QUESTION

select @MED, @MEDNAME

When I include the medication_name piece of the w here clause, I get no returns.  If I take it out I get:

Actoplus MET 15 mg-500 mg Tab -- based on the select top 1 medication name piece.

FOr some reason my
SET @MED= 'Actoplus MET'

doesnt play well with my
and medication_name like '%' + @MED + '%'



Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 4 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros