How to Search and filter "&" sign? - oracle

I need to search for string and extract row data.
In particular, the string has a special character (& sign). How to filter those kinds of strings.
select DISTINCT d.name
from table d
where d.name in ('BUHARY & COMPANY (PVT) LTD','K S KUMARAN')

If you are running your query on sqlplus, keep in mind that & is a special character reserved for variables.
Example
SQL> create table my_test ( c1 varchar2(100) ) ;
Table created.
SQL> insert into my_test values ( 'Hello & Goodbye' ) ;
Enter value for goodbye: ^C
Above, sqlplus stops the execution of the insert because it has found the character ampersand. It asks me to enter the value for that variable. To avoid this, I need to tell sqlplus that I am going to use & as a normal string, for that I have the option define
SQL> set define off
SQL> insert into my_test values ( 'Hello & Goodbye' ) ;
1 row created.
SQL> commit;
Commit complete.
SQL> select * from my_test where c1 like '%&%' ;
C1
--------------------------------------------------------------------------------
Hello & Goodbye
SQL>
If you use another tool, like Toad or Sql Developer, remember to apply the set define off before
Example with Toad

Related

How to replace specified substring with another string for the complete session

I want to create a csv file using sqlplus command for multiple tables as below and it will be called inside shell script.
sqlplus Username/Password#SSD << EOF
set colsep ,
set pagesize 0
set trimspool on
set linesize 32768
set echo off
spool $TABLE.csv;
SELECT * FROM $TABLE;
spool off;
EXIT;
EOF
My Data can also contain a comma(,). if any column data contains comma I want to replace it with "\,".
How can I replace comma for multiple tables and multiple columns? I checked replace function but I don't want to mention column names
As long as you are on SQL Plus 12.2 or above, you should be able to get close to your needs with SET MARKUP
SQL> create table t ( x varchar2(10), y varchar2(10));
Table created.
SQL> insert into t values ('Hello','There');
1 row created.
SQL> insert into t values ('He,llo','The,re');
1 row created.
SQL> set markup csv on
SQL> select * from t;
"X","Y"
"Hello","There"
"He,llo","The,re"
SQL> set markup csv on quote off
SQL> select * from t;
X,Y
Hello,There
He,llo,The,re
You don't get the escape, but if you are enclosing things in quotes, then perhaps that won't matter.

Data in oracle DB has trailing spaces but they're being stripped by sqlplus

I'm retrieving data from an oracle database using sqlplus.
The command is something like this:
select property_name||'|'||property_value from some_table where x = 'foo'
and the data in the database definitely has trailing spaces (this is a thing which causes problems in an application I work with).
When I retrieve that data the spaces have been automatically trimmed somehow. I can see them when I use SQLDeveloper and when retrieved by the application directly.
Is there a way I can stop this happening?
Here is how it should be working.
SQL> create table spaces (blanks varchar2(20));
Table created.
SQL> insert into spaces values ('A');
1 row created.
SQL> insert into spaces values ('A ');
1 row created.
SQL> insert into spaces values ('A ');
1 row created.
SQL> Insert into SPACES (BLANKS) values ('A
B ');
SQL> commit;
Commit complete.
SQL> select blanks, length(blanks), blanks || '!' from spaces;
BLANKS LENGTH(BLANKS) BLANKS||'!'
-------------------- -------------- ---------------------
A 1 A!
A 6 A !
A 3 A !
A 9 A
B B !
SQL>
The last column shows that none of the 'blanks' are being trimmed. Can you share your scenario in your question details? Or try what I've demonstrated and compare.

How to keep SQLPlus from trimming trailing spaces

If I execute the following script in SQLPlus:
CREATE TABLE trailing_spaces (text VARCHAR2(100))
/
-- Note that there is a blank space after 'one' and 'two'
INSERT INTO trailing_spaces (text) VALUES ('one
two
three')
/
COMMIT
/
SQLPlus automatically trims the lines and removes the trailing spaces, so that instead of inserting the value one two three is inserting onetwothree.
Does anyone know how to keep SQLPlus from trimming those lines and execute the script as it is?
You can do it as in SQLPLUS :
SQL> CREATE TABLE trailing_spaces (text VARCHAR2(100));
Table created.
SQL> INSERT INTO trailing_spaces (text) VALUES ('one'||' '||
'two'||' '||
'three') ;
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM TRAILING_SPACES;
TEXT
--------------------------------------------------------------------------------
one two three
If I execute your insert statement, I get the text on three different lines. How could you get the text in one line as onetwothree? I am using Oracle 11.2.0.4
In my case, if I cannot alter the script, I can run the existing script and manipulate it during the selection as shown below.
SQL> insert into tbl1 values('one
2 two');
1 row created.
SQL> insert into tbl1 values('three
2 four');
1 row created.
SQL> select * from tbl1;
TEXT
--------------------
one
two
three
four
SQL> select REPLACE(REPLACE(text, CHR(10)), CHR(13)) as text from tbl1;
TEXT
--------------------
one two
three four

How to set Oracle bind variables when using SQLPlus?

How do I set Oracle bind variables when using SQLPlus?
Example:
SELECT orders.order_no FROM orders WHERE orders.order_date BETWEEN :v1 AND :v2
How do I set the dates of :v1 and :v2?
Notice the following:
VARIABLE is a SQLPlus command. You don't end it with a semicolon (;).
In the VARIABLE command, you do not precede the variable name with
colon (:).
Bind variable can't be of data type "date" - they are some sort of
character value.
For that reason, IN YOUR CODE you must use to_date() with the
proper format model, or some other mechanism, to convert string to
date. That is currently missing in your code. NEVER compare dates to
strings!
Brief demo below.
SQL> variable v1 varchar2(20)
SQL> exec :v1 := '2015/12/22';
PL/SQL procedure successfully completed.
SQL> select 1 as result from dual where to_date(:v1, 'yyyy/mm/dd') < sysdate;
RESULT
----------
1
In common
you may use define and use variable with &
define x = 12 ;
select &x from dual;
Or varable
variable x refcursor;
begin
open :x for select * from dual connect by level < 11;
end;
/
print x

Can I accept values in a VARRAY in PL/SQL directly from the user?

I am looking for something like below for the purpose of accepting array variables from the user itself, I know the below code is wrong, but it is just for the sake of giving the idea of my requirement.
DECLARE
type names_array IS VARRAY(5) OF VARCHAR2(10);
names names_array;
BEGIN
FOR i in 1..5 LOOP
accept names(i);
END LOOP;
FOR j in 1 .. 5 LOOP
dbms_output.put_line(names(j));
END LOOP;
END;
/
It will be difficult to implement using PL/SQL.But we could using substitution variables in SQL Plus.
I create two sql scripts:
the first is main.sql and another is script_insert.sql:
[oracle#db input]$ cat main.sql
accept colu prompt "Please enter value, enter 'done' when no more values: "
set term off verify off
column script new_value v_script
select case '&colu'
when 'done' then ''
else '#script_insert &colu'
end as script
from dual;
set term on
#&v_script.
[oracle#db input]$
[oracle#db input]$ cat script_insert.sql
insert into array_table values ('&1');
#main
[oracle#db input]$
Next we should create a table other than using an array:
SQL> create table array_table(colu varchar2(30));
Table created.
SQL>
Now, we could execute it:
SQL> #main
Please enter value, enter 'done' when no more values: A
1 row created.
Please enter value, enter 'done' when no more values: B
1 row created.
Please enter value, enter 'done' when no more values: Hello
1 row created.
Please enter value, enter 'done' when no more values: "Hello World"
1 row created.
Please enter value, enter 'done' when no more values: done
SQL> select * from array_table;
COLU
------------------------------
A
B
Hello
Hello World
SQL>
We got it,you should using a table other than array because only PL/SQL support it.And you shouldn't using substitution variables in loop!
The finally, why don't you implement this by C/Python/Java in your program?If so, you'll be more relaxed.
Refer:How to create a menu in SQLPlus or PL/SQL

Resources