Db2 how to pass the dynamic variable in LIKE condition

select count(1) into V_COUNT from schema.tablename where pay_id=V_PPCPAYINST_ID and attributename not like '%V_amount%'
with ur

here V_amount is PL/SQL DECIMAL or VARCHAR variable.

If use escape character (/) also it is not working.

how to pass the dynamic variable in LIKE condition

Can any one help me here.
Prardhan NAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tomas Helgi JohannssonCommented:
Hi!

Post the code around this sql-statement.

Regards,
    Tomas Helgi
Pawan KumarDatabase ExpertCommented:
Please try this
SELECT COUNT(*)
     INTO :V_COUNT
     FROM schema.tablename where pay_id=V_PPCPAYINST_ID and attributename not like '%V_amount%'

Open in new window


OR

SELECT COUNT(*)
     INTO V_COUNT
     FROM schema.tablename where pay_id=V_PPCPAYINST_ID and attributename not like '%V_amount%'

Open in new window


And see if you are getting records from this

SELECT COUNT(*)
FROM schema.tablename where pay_id=V_PPCPAYINST_ID and attributename not like '%V_amount%'

Open in new window

Prardhan NAuthor Commented:
My need is about V_amount not V_COUNT
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Tomas Helgi JohannssonCommented:
Hi!

And what data-type is the attributename column ?

Regards,
    Tomas Helgi
Pawan KumarDatabase ExpertCommented:
>>My need is about V_amount not V_COUNT

Is V_amount is a variable or a value ?
Prardhan NAuthor Commented:
here V_amount is PL/SQL DECIMAL or VARCHAR variable.
Tomas Helgi JohannssonCommented:
Hi!

If your attributename column is a decimal type column then applying casting to a like predicate is needed as LIKE is a character based matching predicate.
You could do this something  like this

select count(*) into v_count from schema.tablename where pay_id = VPPCPAYINST_ID and CAST(attributeneme as VARCHAR(<size of  the decimal>) not like '%V_amount%'

Open in new window


where <size of  the decimal> is for example 10 for a column defined as DECIMAL(8,2)



Regards,
    Tomas Helgi
Pawan KumarDatabase ExpertCommented:
Please try with Locate.

select count(*) into V_COUNT from schema.tablename where
pay_id=V_PPCPAYINST_ID and LOCATE(attributename, V_amount) <= 0
Pawan KumarDatabase ExpertCommented:
Or we can also do like below with % and with Locate function.

--

SET V_amount = '%'|| V_amount ||'%';
SELECT COUNT(*)
INTO V_COUNT
FROM schema.tablename where pay_id=V_PPCPAYINST_ID 
and attributename not like (V_amount)

--

Open in new window


OR

--

select count(*) into V_COUNT from schema.tablename where 
pay_id=V_PPCPAYINST_ID and LOCATE(attributename, V_amount) <= 0

--

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tomas Helgi JohannssonCommented:
Hi!

Also note that your attributename column should be in an index as the  NOT LIKE '%V_AMOUNT%' predicate will always cause FULL INDEX SCAN (if column in index ) or in worst case FULL TABLE SCAN and thus causing your query to perform badly.
This is also the case with the LOCATE scalar function suggested by Pawan Kumar.

Regards,
    Tomas Helgi
Prardhan NAuthor Commented:
This is good advice. I need little more improvement for below:

SET V_amount = '%'|| V_amount ||'%';

I finally need to have in my LIKE condition as LIKE V_amount '%_01%'

with above SET command, i am only getting upto %_01%.

I am missing single quotes. I am trying this...

SET V_amount = '''||'%'|| V_amount ||'%'||'''

Will this work ? Need help on this.
Tomas Helgi JohannssonCommented:
Hi!

Try this
SET V_amount = '\"%'|| V_amount ||'%\"';

Open in new window

or
SET V_amount = '\'%'|| V_amount ||'%\'';

Open in new window

Regards,
    Tomas Helgi
Prardhan NAuthor Commented:
Thanks for help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.