I am trying to query a certain row by name in my sql database and it has an ampersand. I tried to set an escape character and then escape the ampersand, but for some reason this isn't working and I'm uncertain as to what exactly my problem is.
Set escape '\'
select * from V1144engine.T_nodes where node_id in(
select node2_id from V1144engine.T_edges where node1_id in(
select node2_id from V1144engine.T_edges where node1_id in(
select node2_id from V1144engine.T_edges where node1_id =
(select node_id from V1144engine.T_nodes where node_name = 'Geometric Vectors \& Matrices')))
and edge_type_id = 1)
and node_type_id = 1
and node_id in (
select node2_id from V1144engine.T_edges where node1_id =
(select node_id from V1144engine.T_nodes where node_name = 'Algebra II')
and edge_type_id = 2);
Although this has a similar solution to this question, the problems are posed very differently. They may end up having the same solution, but that does not mean that the questions are the same.
In order to escape &
you can try following ways:-
set scan off
set define off
set escape on
then replace &
by/&
&
by &&
One of them should work atleast.
Additionally if you are using PL/SQL developer then there is & icon in bottom of sql window please go there and disable it. Note in older version this option is not present.
Also make sure set define off is written in very beginning of the script.
--SUBSTITUTION VARIABLES
-- these variables are used to store values TEMPorarily.
-- The values can be stored temporarily through
-- Single Ampersand (&)
-- Double Ampersand(&&)
-- The single ampersand substitution variable applies for each instance when the
--SQL statement is created or executed.
-- The double ampersand substitution variable is applied for all instances until
--that SQL statement is existing.
INSERT INTO Student (Stud_id, First_Name, Last_Name, Dob, Fees, Gender)
VALUES (&stud_Id, '&First_Name' ,'&Last_Name', '&Dob', &fees, '&Gender');
--Using double ampersand substitution variable
INSERT INTO Student (Stud_id,First_Name, Last_Name,Dob,Fees,Gender)
VALUES (&stud_Id, '&First_Name' ,'&Last_Name', '&Dob', &&fees,'&gender');
set escape on
... node_name = 'Geometric Vectors \& Matrices' ...
or alternatively:
set define off
... node_name = 'Geometric Vectors & Matrices' ...
The first allows you to use the backslash to escape the &.
The second turns off & "globally" (no need to add a backslash anywhere). You can turn it on again by set define on
and from that line on the ampersands will get their special meaning back, so you can turn it off for some parts of the script and not for others.
straight from oracle sql fundamentals book
SET DEFINE OFF
select 'Coda & Sid' from dual;
SET DEFINE ON
how would one escape it without setting define.
This works as well:
select * from mde_product where cfn = 'A3D"&"R01'
you define &
as literal by enclosing is with double qoutes "&"
in the string.
You can use
set define off
Using this it won't prompt for the input
Escape is set to \
by default, so you don't need to set it; but if you do, don't wrap it in quotes.
Ampersand is the SQL*Plus substitution variable marker; but you can change it, or more usefully in your case turn it off completely, with:
set define off
Then you don't need to bother escaping the value at all.
I wrote a regex to help find and replace "&" within an INSERT, I hope that this helps someone.
The trick was to make sure that the "&" was with other text.
Find “(\'[^\']*(?=\&))(\&)([^\']*\')”
Replace “$1' || chr(38) || '$3”
I know it sucks. None of the above things were really working, but I found a work around. Please be extra careful to use spaces between the apostrophes [ ' ], otherwise it will get escaped.
SELECT 'Hello ' '&' ' World';
Hello & World
You're welcome ;)
REPLACE(<your xml column>,'&',chr(38)||'amp;')
Source: Stackoverflow.com