How can I write an insert statement which includes the & character? For example, if I wanted to insert "J&J Construction" into a column in the database.
I'm not sure if it makes a difference, but I'm using Oracle 9i.
The short answer is to use two single quotes - '' - in order for an SQL database to store the value as ' .
Answers. A string literal is enclosed in single quotes. If you want to embed a single quote within the literal (start, middle or end) just use two quotes instead of your single quote.
I keep on forgetting this and coming back to it again! I think the best answer is a combination of the responses provided so far.
Firstly, & is the variable prefix in sqlplus/sqldeveloper, hence the problem - when it appears, it is expected to be part of a variable name.
SET DEFINE OFF will stop sqlplus interpreting & this way.
But what if you need to use sqlplus variables and literal & characters?
e.g.
set define on set escape on define myvar=/forth select 'back\\ \& &myvar' as swing from dual;
Produces:
old 1: select 'back\\ \& &myvar' from dual new 1: select 'back\ & /forth' from dual SWING -------------- back\ & /forth
If you want to use a different escape character:
set define on set escape '#' define myvar=/forth select 'back\ #& &myvar' as swing from dual;
When you set a specific escape character, you may see 'SP2-0272: escape character cannot be alphanumeric or whitespace'. This probably means you already have the escape character defined, and things get horribly self-referential. The clean way of avoiding this problem is to set escape off first:
set escape off set escape '#'
If you are doing it from SQLPLUS use
SET DEFINE OFF
to stop it treading & as a special case
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With