How to update value with special character in oracle 11g - oracle

I want to update the password having special characters ''?# #C $4 ABC (starting two characters are two single quotes) in Xyz table.
I am trying the following query
UPDATE Xyz set password="''?# #C $4" where user_no like '%123%';
But I am getting error as
ORA-00911: invalid charachter

The q-quoting mechanism helps in such situations, when you have to work with multiple single quotes within the string.
SQL> desc xyz
Name Null? Type
----------------------------------------- -------- ----------------------------
USER_NO NUMBER
PASSWORD VARCHAR2(20)
SQL> select * From xyz;
USER_NO PASSWORD
---------- --------------------
123 a
SQL> update xyz set password = q'[''?# #C $3]' where user_no = 123;
1 row updated.
SQL> select * From xyz;
USER_NO PASSWORD
---------- --------------------
123 ''?# #C $3
SQL>

Are you pasting the query from a different editor or IDE ? or Maybe copying from windows applications to Linux? In that case, there may be non-printable characters present.
If so, you could retype (not copy-paste) the SQL statement and try.
Also, double quotes aren't commonly used in SQL. You may want to replace them with single quotes.

Related

How to remove specific initial characters from a column in oracle?

I have column by the name of phone_number and it consists different types of numbers for example:
phone_number
078912354
93784385483
009378248448
776868886
So I want to remove all the initial numbers which starts with(0,93,0093). The expected result which I want is:
phone_number
78912354
784385483
78248448
776868886
Here's one option:
Sample data:
SQL> select * from test order by phone_number;
PHONE_NUMBER NEW_PHONE_NUMBER
------------ --------------------
009378248448
078912354
776868886
93784385483
Remove leading characters you mentioned:
SQL> update test set
2 new_phone_number = regexp_replace(phone_number, '^(0093|093|93|0)');
4 rows updated.
Result:
SQL> select * from test order by phone_number;
PHONE_NUMBER NEW_PHONE_NUMBER
------------ --------------------
009378248448 78248448
078912354 78912354
776868886 776868886
93784385483 784385483
SQL>

Hidden Double Quotes in Oracle Sql Developer Application

We are getting hidden double quotes for multiline char column values in Oracle sql application. How to fix this problem? Can some one help us on this?
Example:
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBCC"
This is the default behavior of SQL Developer.
Whenever you copy the data from the result of the query and the value is a multiline value then it uses the double quote as a wrapper to the value but it doesn't mean that there are double quotes in the actual data.
See this:
SQLPLUS
SQL> SELECT * FROM CUSTOMERS;
CUSTOMER_ID CUSTOMER_NAME
----------- --------------------------------------------------
1 tejash
Copy pasting the result from the SQL Developer:
SQL Developer
1 tejash
Now, Let's add some multiline value in the CUSTOMER_NAME column.
SQLPLUS
SQL> INSERT INTO CUSTOMERS VALUES
2 (2,
3 'TEJASH
4 SO
5 EXAMPLE');
1 row created.
SQL> SELECT * FROM CUSTOMERS;
CUSTOMER_ID CUSTOMER_NAME
----------- --------------------------------------------------
1 tejash
2 TEJASH
SO
EXAMPLE
SQL> COMMIT;
Commit complete.
SQL>
Copy pasting the result from the SQL Developer:
SQL Developer
1 tejash
2 "TEJASH
SO
EXAMPLE" -- wrapped in double quotes

How do I export oracle query result without enclosure

I have oracle 12c and trying to export query result to csv or text file but I dont want any enclosure of my data. I have tried SET SQLFORMAT csv which creates csv file but data comes in double quotes then I tried SET SQLFORMAT delimited | but that also comes with double quoted. I also tried SET MARKUP csv on delimeter | quote off it also gave me same result. I dont think MARKUP command works on 12c but it did not give me error. Here is my script:
SET SQLFORMAT delimited | ;
spool 'C:\Temp\MyResults.csv';
select 1 AS Col1, 'Data Line 1' AS Col2 from dual UNION select 2 AS Col1, 'Data Line 2' AS Col2 from dual;
spool off;
This gives me result:
"COL1"|"COL2"
1|"Data Line 1"
2|"Data Line 2"
But I want without double quotes on string data.
COL1|COL2
1|Data Line 1
2|Data Line 2
I would appreciate if someone can give me any poption.
Thanks.
I have the following working with Oracle 19 (client and server on Linux):
SQL> set markup CSV on quote off
SQL> desc t;
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECT_ID NUMBER
OBJECT_NAME VARCHAR2(128)
SQL> select * from t where rownum=1;
OBJECT_ID,OBJECT_NAME
16,TS$
SQL> set markup csv on quote on
SQL> select * from t where rownum=1;
"OBJECT_ID","OBJECT_NAME"
16,"TS$"
SQL>

When or Why to use a "SET DEFINE OFF" in Oracle Database

I'm watching a Script in Oracle and I see something I don't recognize
REM INSERTING into database1."Users"
SET DEFINE OFF;
Insert into database1."Users" ("id","right") values ('1','R');
I'm looking for documentation about "set define off" and it's literally writing "disable the parsing of commands to replace substitution variable with their values"
I don't really understand what they want to say.
Can anyone help me?
By default, SQL Plus treats '&' as a special character that begins a substitution string. This can cause problems when running scripts that happen to include '&' for other reasons:
SQL> insert into customers (customer_name) values ('Marks & Spencers Ltd');
Enter value for spencers:
old 1: insert into customers (customer_name) values ('Marks & Spencers Ltd')
new 1: insert into customers (customer_name) values ('Marks Ltd')
1 row created.
SQL> select customer_name from customers;
CUSTOMER_NAME
------------------------------
Marks Ltd
If you know your script includes (or may include) data containing '&' characters, and you do not want the substitution behaviour as above, then use set define off to switch off the behaviour while running the script:
SQL> set define off
SQL> insert into customers (customer_name) values ('Marks & Spencers Ltd');
1 row created.
SQL> select customer_name from customers;
CUSTOMER_NAME
------------------------------
Marks & Spencers Ltd
You might want to add set define on at the end of the script to restore the default behaviour.
Here is the example:
SQL> set define off;
SQL> select * from dual where dummy='&var';
no rows selected
SQL> set define on
SQL> /
Enter value for var: X
old 1: select * from dual where dummy='&var'
new 1: select * from dual where dummy='X'
D
-
X
With set define off, it took a row with &var value, prompted a user to enter a value for it and replaced &var with the entered value (in this case, X).

sqlplus pass a string containing ' ' such as 'index1','index2','index3'

I need to pass this value to my sql file cause I am executing then a where condition with a IN.
For instance : Delete FROM table WHERE col IN ('index1','index2','index3')
I have an issue when I try to call this sql file from cmd using sqlplus command
set INDEXES = 'index1','index2','index3'
sqlplus script %INDEXES%
When I do that, only index1 is passed or there is a problem
I tried to do that
set INDEXES = "'index1','index2','index3'"
sqlplus script %~INDEXES%
but there is a problem too
Here is my sql:
Delete FROM table WHERE col IN (&1)
Do you have any idea how I can successfully pass the string I need ?
thank you
Oracle does not come with a built-in string tokenizer. So, we have to build our own. There are several different solutions on SO. Here is one I published, which will work on 10g or higher. For earlier versions try this one.
Actually, your technique is correct.
sqlplus scott/tiger #script.sql "'index1','index2','index3'"
where script.sql is:
Delete FROM table WHERE col IN (&1)
will result in &1 being replaced, verbatim, with 'index1','index2','index3', resulting in sqlplus executing:
Delete FROM table WHERE col IN ('index1','index2','index3')
The problem i see is that the delete statement doesn't end in a semi-colon and the script doesn't commit/exit (maybe those were just excluded in your post).
So it follows that, if your command-line properly interpolates environment variables, then
set INDEXES = "'index1','index2','index3'"
sqlplus scott/tiger #script.sql %~INDEXES%
results in the same command as the first in my comment.
An easy way to see what sqlplus is doing with the command-line parameters is to simply add prompt to the beginning of the delete line in your script:
prompt Delete FROM table WHERE col IN (&1)
I would look at this as a variable in list question. These can be tricky and the answer varies based on the version of Oracle you have access to
create table aaa(aaa varchar2(50));
insert into aaa values('index1');
insert into aaa values('index2');
insert into aaa values('index3');
insert into aaa values('index4');
insert into aaa values('index5');
declare
pindexes varchar2(100) ;
begin
pindexes := 'index1,index2,index3';
delete aaa where aaa in (
select substr(pindexes,
loc+1,
nvl(
lead(loc) over (order by loc) - loc-1,
length(pindexes) - loc)
)
from (
select distinct (instr(pindexes, ',', 1, level)) loc
from dual
connect by level < length(pindexes)
)
);
end ;
/
select * from aaa;
/
--drop table aaa;
this way you just pass in your string as 'index1,index2,index3'
this should work 9i+
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:210612357425

Resources