57

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?

Hash
  • 4,647
  • 5
  • 21
  • 39
Rachel
  • 100,387
  • 116
  • 269
  • 365

13 Answers13

84

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.

qyb2zm302
  • 6,458
  • 2
  • 17
  • 17
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
  • 10
    17 seconds... I was *so* close – OMG Ponies Nov 14 '10 at 00:14
  • 1
    so will this only turn defination off for my sql or for the complete table or complete database also can i have something like this in production insert script ? – Rachel Nov 14 '10 at 00:14
  • IMO, your production code should be *using* substitution, or at minimum, handling it itself. I believe that code does the setting for your login session, though. – Andrew Barber Nov 14 '10 at 00:23
  • @Rachel - SET commands are client directives that have session scope. The database has no idea what directives, if any, you've given your client so it has no impact on a table or on any other database session. – Justin Cave Nov 14 '10 at 00:32
  • 3
    @OMG Ponies- Sweet, sweet vengeance for all the times you've beaten me :-) – Justin Cave Nov 14 '10 at 00:33
  • @Justin: but in that case, lets say if am running the insert script with set directive off in production than will it be only for one insert or for the complete list of inserts for that particular session, can explain a little more on this, am only concern is `should i have set directive off in the insert script which will be running on production ?` – Rachel Nov 14 '10 at 00:36
  • 2
    @Rachel - It's perfectly fine to SET DEFINE OFF in a production script, yes. It will affect all the other statements running in the same session (you could, of course, SET DEFINE ON later in the script if you wanted to). But it would generally be unusual to have substitution variables in a script running in production-- you normally don't want production scripts to prompt the user for information, you normally want that to be part of the script. – Justin Cave Nov 14 '10 at 01:44
  • @Justin: Thanks Justin for Clarification. – Rachel Nov 14 '10 at 14:54
26

Try 'Java_22 '||'&'||' Oracle_14'

assylias
  • 321,522
  • 82
  • 660
  • 783
Eternal Noob
  • 2,717
  • 5
  • 27
  • 41
22

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'); 
Craig
  • 5,740
  • 21
  • 30
7

you can simply escape & by following a dot. try this:

INSERT INTO STUDENT(name, class_id) VALUES ('Samantha', 'Java_22 &. Oracle_14');
Jerry
  • 316
  • 5
  • 11
3

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.

Chikku Jacob
  • 2,114
  • 1
  • 18
  • 33
1

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.

Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40
lakhcoool
  • 21
  • 1
1

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 !!!

0

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)||'');
Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
Surya
  • 11
  • 2
0

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

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
0

Also you can use concat like this :D

Insert into Table Value(CONCAT('JAVA ',CONCAT('& ', 'Oracle'));
Luis Cardoza Bird
  • 1,265
  • 4
  • 24
  • 43
0
strAdd=strAdd.replace("&","'||'&'||'");
Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40
Gank
  • 4,507
  • 4
  • 49
  • 45
-1

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.

Juan Pablo
  • 1,213
  • 10
  • 15
-4

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}'

Maria Ioannidou
  • 1,544
  • 1
  • 15
  • 36