This question already has answers here:
How do I ignore ampersands in a SQL script running from SQL Plus?
(7 answers)
Closed 5 years ago.
I want to insert special character & in my insert statement. My insert is:
INSERT INTO STUDENT(name, class_id) VALUES ('Samantha', 'Java_22 & Oracle_14');
If I try to run this query I am getting a popup and it asks me to enter value for Oracle_14.
How can I enter special characters like & in the insert statement for oracle db?
If you are in SQL*Plus or SQL Developer, you want to run
SQL> set define off;
before executing the SQL statement. That turns off the checking for substitution variables.
SET directives like this are instructions for the client tool (SQL*Plus or SQL Developer). They have session scope, so you would have to issue the directive every time you connect (you can put the directive in your client machine's glogin.sql if you want to change the default to have DEFINE set to OFF). There is no risk that you would impact any other user or session in the database.
Try 'Java_22 '||'&'||' Oracle_14'
Justin's answer is the way to go, but also as an FYI you can use the chr() function with the ascii value of the character you want to insert. For this example it would be:
INSERT INTO STUDENT(name, class_id) VALUES ('Samantha', 'Java_22 '||chr(38)||' Oracle_14');
you can simply escape & by following a dot. try this:
INSERT INTO STUDENT(name, class_id) VALUES ('Samantha', 'Java_22 &. Oracle_14');
To Insert values which has got '&' in it. Use the folloiwng code.
Set define off;
Begin
INSERT INTO STUDENT(name, class_id) VALUES ('Samantha', 'Java_22 & Oracle_14');
End ;
And Press F5 from Oracle or Toad Editors.
There are 3 ways to do so :
1) Simply do SET DEFINE OFF; and then execute the insert stmt.
2) Simply by concatenating reserved word within single quotes and concatenating it.
E.g. Select 'Java_22 ' || '& '|| ':' || ' Oracle_14' from dual --(:) is an optional.
3) By using CHR function along with concatenation.
E.g. Select 'Java_22 ' || chr(38)||' Oracle_14' from dual
Hope this help !!!
We can use another way as well
for example to insert the value with special characters 'Java_22 & Oracle_14' into db we can use the following format..
'Java_22 '||'&'||' Oracle_14'
Though it consider as 3 different tokens we dont have any option as the handling of escape sequence provided in the oracle documentation is incorrect.
If an escape character is to be added at the beginning or the end like "JAVA", then use:
INSERT INTO STUDENT(name, class_id) VALUES ('Samantha', ''||chr(34)||'JAVA'||chr(34)||'');
For special character set, you need to check UNICODE Charts. After choose your character, you can use sql statement below,
SELECT COMPOSE('do' || UNISTR('\0304' || 'TTTT')) FROM dual;
--
dōTTTT
Also you can use concat like this :D
Insert into Table Value(CONCAT('JAVA ',CONCAT('& ', 'Oracle'));
strAdd=strAdd.replace("&","'||'&'||'");
In my case I need to insert a row with text 'Please dial *001 for help'.
In this case the special character is an asterisk.
By using direct insert using sqlPlus it failed with error "SP2-0734: unknown command beginning ... "
I tryed set escape without success.
To achieve, I created a file insert.sql on filesystem with
insert into testtable (testtext) value ('Please dial *001 for help');
Then from sqlPlus I executed
#insert.sql
And row was inserted.
You can either use the backslash character to escape a single character or symbol
'Java_22 \& Oracle_14'
or braces to escape a string of characters or symbols
'{Java_22 & Oracle_14}'
Related
This question already has answers here:
How to avoid variable substitution in Oracle SQL Developer
(5 answers)
Closed 3 months ago.
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);
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.
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.
You can use
set define off
Using this it won't prompt for the input
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.
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 at least.
additionally if you are using PL/SQL developer then there is & icon in the bottom of SQL window please go there and disable it. Note in the older version this option is not present.
Also make sure set define off is written at the very beginning of the script.
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.
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.
REPLACE(<your xml column>,'&',chr(38)||'amp;')
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”
--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');
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 ;)
I have a database in an oracle, I filled in the "Number" fields with numbers starting from "A14602727" to "A14603000" but it turned out that I accidentally typed the symbol A (in Ukrainian) instead of A (in English). And now I when find the command:
select number from test where number 'А14602727'
...find me nothing. Is it possible somehow with the help of the command to replace all numbers from "A14602727" (Ukrainian) to "A14602727" (English) ?
I will be grateful for your help!)
You can use following trick to convert any character to its base character using accent-insesitive binary sorting.
select your_col,
utl_raw.cast_to_varchar2(nlssort(your_col, 'nls_sort=binary_ai')) converted_col
from your_table
Use it in update statement accordingly.
Cheers!!
You could use regexp_replace():
update test set number = regexp_replace(number, '^Ä', 'A');
Regexp '^Ä' represents character 'Ä' at the beginning of the string. I used 'Ä' to represent the A in Ukrainian: replace this with the correct character that you want to replace.
This can also be done, probably more efficiently, with substr and like:
update test set number = 'A' || substr(number, 2) where number like 'Ä%';
Use the simple Oracle REPLACE function:
UPDATE test
SET number = REPLACE(number, 'A', 'A');
Replace is taking 3 parameters:
String in which you will search for a string to replace (The number column)
The string you are searchin gto replace (Ukrainian A)
The string you will replace it with (English A)
Here you have a DEMO example.
Also please note that the query in your question:
select number from test where number 'А14602727';
Is not valid. It should be something like this:
select number from test where number like 'А14602727';
or like this:
select number from test where number = 'А14602727';
So first do check that!
Cheers!
CREATE TABLE test (a VARCHAR(2000))
INSERT INTO test
VALUES ('This is a AB_CD_test.dbo.ABC'),
('This is a AB_CD.dbo.ABC')
Table before running Update query:
This is a AB_CD_test.dbo.ABC
This is a AB_CD.dbo.ABC
This is a [AB_CD].[dbo].[XYZ]
I am trying to replace the keyword AB_CD with AB_CD_test using this query:
UPDATE [US\AF83767].[test]
SET a = REPLACE(a, 'AB_CD', 'AB_CD_test')
WHERE a LIKE '%AB!_CD%' ESCAPE '!';
But as expected, data changes to:
This is a AB_CD_test_test.dbo.ABC
This is a AB_CD_test.dbo.ABC
This is a [AB_CD_test].[dbo].[XYZ]
The second is fine but the first should remain 'This is a AB_CD_test.dbo.ABC'.
Could anyone help with a query which does not change the first row and just looks for occurrences of 'AB_CD'.
Relatively new to SQL and would appreciate your help.
It's not exactly clear what you want to prevent. Do you only want to prevent the replacement when "Test" is present, or any string between "AB_CD" and ".dbo"
I'm guessing the latter, so I would suggest this:
UPDATE [US\AF83767].[test]
SET a = REPLACE(a, 'AB_CD', 'AB_CD_test')
WHERE a LIKE '%AB!_CD.%' ESCAPE '!';
I think this will do what you want:
UPDATE #test
SET a = REPLACE(REPLACE(a, 'AB_CD_test', 'AB_CD'), 'AB_CD', 'AB_CD_test')
WHERE a LIKE '%AB!_CD%' ESCAPE '!';
It replaces all occurrences of AB_CD_test to AB_CD... and then changes all AB_CD to AB_CD_test.
It appears that you want to replace instances of AB_CD ONLY WHEN it is followed by a period or a closing bracket or nothing at all.
where col like '%AB!_CD[.!]]%' ESCAPE '!' or col like '%AB!_CD' ESCAPE '!';
I need to include the special character "%" in my LIKE clause in a SQL query, e.g.:
Select * From Some_Table Where Field_Name Like 'bla%bla&2';
How do I write that?
If you want to match Field_Name values that contain 'bla%bla&2', then you need to write this:
set define off
Select * From Some_Table Where Field_Name Like '%bla\%bla&2%' escape '\';
You get to specify which character you want to use to escape a following character (thanks should go to mathguy, not me). You also have to set define off to prevent sqlplus from trying to substitute values in a string.
If, however, you want to match Field_Name values that exactly equal the given string, then you do this instead:
set define off
Select * From Some_Table Where Field_Name = 'bla%bla&2';
If I am not mistakend you escape them with a backslash (\)
Select * From Some_Table Where Field_Name Like 'bla\%bla&2' ESCAPE '\';
Use escape \ to treat is a literal
SELECT *
FROM Some_Table
WHERE Field_Name LIKE 'blah\%'|| 'blah' ||'&'|| '2';
I'll guess that you're using a tool which treats &n, where n is a digit, as a variable marker. If you're using SQL*Plus or SQL Developer you'd need to issue the SQL*Plus command SET DEFINE OFF. Other tools may use other methods to accomplish this.
Best of luck.
I do not think the backslash escape character will work here for the ampersand. Instead, you will want to divide your search into concatenated strings using double pipes. Use single quotes around each piece of literal text. Next, replace the & with chr(38) which is the ampersand. You can see this with:
select chr(38) from dual
You will still want to include the backslash before the % and finish your statement with escape '\'. Notice, I did not quote the chr(38).
select * From Some_Table Where Field_Name Like 'bla\%bla'||chr(38)||'bla' escape '\'
Hey I'm using oracle DB with Iron Python and I'm having trouble with strings that contains the char " ' " like in Mc'donalds. (I know it is looking for the closing ' )
The string is accepted from the user input and I want to add it to my DB as it is, meaning without omitting or changing any character.
How can I do it?
Try using the "q" (quote) function:
INSERT INTO restaurantTable (name)
VALUES (q'[O'Reilly and Conway's Irish Pub]');
You can also double-up the single apostrophes (O''Reilly and Conway''s Irish Pub). But in your case you'd have to parse them out, so either using the quote function or query parameters would work the best.
For more information: Q-quote operator introduced in Oracle 10g
Taken from PL/SQL, how to escape single quote in a string?
You can do it either using Q quoting like
q'[insert into MY_TBL (Col) values('ER0002')]';
OR you can use two quotes to denote a single quote
'insert into MY_TBL (Col) values(''ER0002'')';