问题
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.
回答1:
Instead of
node_name = 'Geometric Vectors \& Matrices'
use
node_name = 'Geometric Vectors ' || chr(38) || ' Matrices'
38 is the ascii code for ampersand, and in this form it will be interpreted as a string, nothing else. I tried it and it worked.
Another way could be using LIKE and an underline instead the '&' character:
node_name LIKE 'Geometric Vectors _ Matrices'
The chance that you'll find some other record too, which is different in only this one character, is quite low.
回答2:
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.
回答3:
You can use
set define off
Using this it won't prompt for the input
回答4:
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.
回答5:
In order to escape &
you can try following ways:-
set scan off
set define off
set escape on
then replace&
by/&
- replace
&
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.
回答6:
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.
回答7:
REPLACE(<your xml column>,'&',chr(38)||'amp;')
回答8:
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.
来源:https://stackoverflow.com/questions/12961215/escaping-ampersand-character-in-sql-string