single quote problem

The following code doesn't work.  I have to use a defined varible my_long_string and the value could be anything like TE'ST, TES'T ....   Is there a simple way to make this work in the select statement and find my_desc ='TE'ST';?  Thanks.

define my_long_string='TE'ST';
select * from emp  where  emp_desc = '&my_long_string';
Who is Participating?
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.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
what about this:

define my_long_string='TE\'ST';
define my_long_string='TE''ST';
ewang1205Author Commented:
Doesn't work.  Have you tried?  
ewang1205Author Commented:
I also tried the following and not working.
define my_long_string='TE'''ST';
define my_long_string='TE''''ST';
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Replace any single quote contained in the variable with a double quote before using in the query statement. This is how it is done in SQL, not sure about Oracle.

The attached example is c#


Open in new window

SQL> define test2='TE'''||''||'''ST'
Naveen KumarProduction Manager / Application Support ManagerCommented:
Please see the below. It is working fine for me. Can you try the below :

SQL> define my_long_string=TE''ST                      
-- two single quotes and not a double quote.
SQL> select * from emp  where  emp_desc = '&my_long_string';


For your understanding, it is working as shown below for me. I have used some temp table to query as you have shown.

SQL> define my_long_string=TE''ST                      
-- two single quotes and not a double quote.

SQL> desc test
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 TESTCOL                                            VARCHAR2(20)

SQL> insert into test values('TE''ST');

1 row created.

SQL> select * from test;


SQL> select * from test where testcol = '&my_long_string';
old   1: select * from test where testcol = '&my_long_string'
new   1: select * from test where testcol = 'TE''ST'



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
declare @my_long_string varchar(25)
set @my_long_string = 'Tes''T'
Jinesh KamdarCommented:
define my_long_string='TE' || CHR(39) || 'ST';
select * from emp  where  emp_desc = '&my_long_string';
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
Oracle Database

From novice to tech pro — start learning today.