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';
ewang1205Asked:
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';
0
ewang1205Author Commented:
Doesn't work.  Have you tried?  
0
ewang1205Author Commented:
I also tried the following and not working.
define my_long_string='TE'''ST';
define my_long_string='TE''''ST';
0
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!

rhythmluvrCommented:
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#
 

myvariable.Replace("'","''");

Open in new window

0
jwittenmCommented:
SQL> define test2='TE'''||''||'''ST'
0
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;

TESTCOL
--------------------
TE'ST

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'

TESTCOL
--------------------
TE'ST

SQL>
0

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