Avatar of robthomas09
robthomas09
 asked on

SQL - Select like statement with variables

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 @MEDNAME = null
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 + '%'

Thoughts?

Thanks!
Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Member_2_861731

8/22/2022 - Mon
SOLUTION
Aneesh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
David L. Hansen

Your final SELECT statement (SELECT @MED, @MEDNAME) would resolve to:
SELECT 'Actoplus MET', null

That is suspicious to me because you are selecting literals instead of data from a table...
robthomas09

ASKER
If I select:
             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'

I get :

Actoplus MET 15 mg-500 mg Tab

BUT  if I select

DECLARE @MED char(200)
SET @MED= 'Actoplus'

             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 + '%'

I get no records returned.  CRAZY
SOLUTION
mastoo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Ephraim Wangoya

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Member_2_861731

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck