How to remove blank spaces from SQLPLUS? - shell

I have a file with this:
set linesize 1000
set trimspool on
set trimout on
set pagesize 0
set feedback off
spool result.csv
SELECT process_id || ';' || file_name || ';' || source || ';' || destination || ';' || type || ';' || transfer_name || ';' || message || ';' || message2 || ';' || destination_sub_group
FROM table
WHERE process_id = '12345';
And SQLPLUS is calling it
But this is returning blank spaces, specially message2 field, any idea on how to remove it?
Here is the output:
12345;filename.txt;X;X;4;X;xx = xxxx
Warning: Using insecure memory!
Decoding data....
Secret key is required to read it.
Key for user ID "X"
Error decrypting file '/apps/egs/gen/file_name.txt.pgp'.
;INBOUND
Thanks!
I replaced some values with X.
Here is the output I would like:
12345;filename.txt;X;X;4;X;xx = xxxx Warning: Using insecure memory! Decoding data.... Secret key is required to read it. Key for user ID "X" Error decrypting file /apps/egs/gen/file_name.txt.pgp'.;INBOUND

i fought this problem for days, when i wanted to get the query results into a csv...
set mark csv ON ended up solving the problem in that case... took my for ever to find that command

Try using TRIM to remove trailing and leading spaces and REPLACE to remove linefeeds and carriage returns:
SELECT process_id || ';' || file_name || ';' || source || ';' || destination || ';' || type || ';' || transfer_name || ';' || message || ';' ||
replace(replace(trim(message2),CHR(10),' '),CHR(13),' ') || ';' || destination_sub_group
FROM table WHERE process_id = '12345';

Add the following:
COL the_stuff form a1000
Then add the "the_stuff" column alias to your query:
SELECT process_id .... destination_sub_group the_stuff from ..
This will explicitly allow you some control over how this output is displayed.
Next, to deal with the embedded linefeeds in your output, wrap lot around a TRANSLATE ( ..., CHR(10), '+' )
E.g.
This show data with linefeeds:
SQL> select 'line 1' || chr(10) || 'line2' || chr(10) || 'line 3' txt from dual;
TXT
-------------------
line 1
line2
line 3
Translate to replace the linefeeds with "+":
SQL> select translate ( 'line 1' || chr(10) || 'line2' || chr(10) || 'line 3',
chr(10), '+' ) txt
from dual;
TXT
-------------------
line 1+line2+line 3
SQL>

Related

CSV File generation in Oracle

I am new to oracle and here I am trying generate a CSV file from the values from my table and mail it. I am able to generate the csv file but I am unable to add a heading to my file and my date values are being shown as ########. I want the CSV file to be generated as the expected one .
The expected CSV :
The generated CSV :
Here is my code :
l_attach_text_h :=
'RECORD_ID ,INPUTTED DATE ,INPUTTED BY ,BROKER .....
FOR employee_rec in c1
LOOP
l_attach_text := '"' ||
employee_rec.FC_ED_RECORD_ID || '","' ||
employee_rec.FC_ED_UPLOADTIME || '","' ||
employee_rec.FC_ED_USER_ID || '","' ||
employee_rec.FC_ED_BROKER || '","' ||
........
l_clob := l_clob||chr(10)||l_attach_text;
END LOOP;
l_clob := l_attach_text_h ||chr(13)|| l_clob;
DBMS_OUTPUT.put_line(' Dtls processing completed...');
v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.Mail(v_Mail_Conn, v_From);
utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
utl_smtp.Data(v_Mail_Conn,
'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || v_From || crlf ||
'Subject: '|| v_Subject || crlf ||
'To: ' || v_Recipient || crlf ||
'MIME-Version: 1.0'|| crlf || -- Use MIME mail standard
'Content-Type: multipart/mixed;'|| crlf ||
' boundary="-----SECBOUND"'|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
'Content-Transfer_Encoding: 7bit'|| crlf ||
crlf ||
'Please find the following in the attachments :'|| crlf || -- Message body
'Entry details & Entry details'|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
' name="Files.csv"'|| crlf ||
'Content-Transfer_Encoding: 8bit'|| crlf ||
'Content-Disposition: attachment;'|| crlf ||
' filename="Files.csv"'|| crlf ||
crlf ||
l_clob || crlf || -- Content of attachment
crlf ||
'-------SECBOUND--' -- End MIME mail
);
utl_smtp.Quit(v_mail_conn);
DBMS_OUTPUT.put_line('mail send completed...');
can anyone help me with this?
As of the heading: create a new comma-separated line which contains all headings, e.g.
SQL> select '"Record ID"' ||','|| '"Inputted Date"' ||','|| '"Inputted By"' as heading from dual;
HEADING
-----------------------------------------
"Record ID","Inputted Date","Inputted By"
SQL>
and include it as the 1st line in the output file.
As of the ###### issue: are you sure it is a problem? What happens when you double-click in between columns B and C in Excel? That operation should adjust column B width so that it matches data width (so I'd expect actual data to be seen afterwards).

how to insert database column names in CSV file in shell script

I am executing below script against oracle database.
sqlplus -s $DBUSER/$DBPASS#$DBHOST:$DBPORT/$DBSID << FIN_SQL > $pi_cancellation.csv
SET HEADING ON
SET FEEDBACK OFF
SET LINESIZE 32767
SET PAGESIZE 0
SET SERVEROUTPUT OFF
SELECT ROOT_PROC_INS_ID || ',' || PROCESS_INSTANCE || ',' || PROCESS_TEMPLATE || ',' || START_TIME || ',' || STATUS || ',' || ATTRIBUTE2 || ',' || ATTRIBUTE14
FROM BPMUSER.EC_PE_STATUS
WHERE START_TIME<(SYSDATE - 15) AND (STATUS='pi_halted' OR STATUS='pi_failed')
ORDER BY START_TIME DESC;
FIN_SQL
I want to insert column names into CSV file but not as given in sql query above but as shown below. For e.g. Order_Ref should be inserted in CSV file for Attribute2
ROOT_PROCESS_INSTANCE,PROCESS_INSTANCE,PROCESS_NAME,START_DATE,STATUS,ORDER_REF,LINE_NUMBER
pvm:0a12dd82,pvm:0a12dd84,TechnicalErrorProcess,21-JUN-19 07.01.58.560000 AM,pi_halted,38930,1
pvm:0a12dd77,pvm:0a12dd79,TechnicalErrorProcess,20-JUN-19 12.36.27.384000 PM,pi_halted,1572846,1
pvm:0a12dd6t,pvm:0a12dd6v,TechnicalErrorProcess,20-JUN-19 12.05.22.145000 PM,pi_halted,38929,1
pvm:0a12dd4h,pvm:0a12dd4l,TechnicalErrorProcess,17-JUN-19 07.11.43.522000 AM,pi_halted,9973686,1
Echo the header row before running SQL*Plus, and turn its heading off.
echo 'ROOT_PROCESS_INSTANCE,PROCESS_INSTANCE,PROCESS_NAME,START_DATE,STATUS,ORDER_REF,LINE_NUMBER' > $pi_cancellation.csv
sqlplus -s $DBUSER/$DBPASS#$DBHOST:$DBPORT/$DBSID << FIN_SQL >> $pi_cancellation.csv
SET HEADING OFF
SET FEEDBACK OFF
SET LINESIZE 32767
SET PAGESIZE 0
SET SERVEROUTPUT OFF
SELECT ROOT_PROC_INS_ID || ',' || PROCESS_INSTANCE || ',' || PROCESS_TEMPLATE || ',' || START_TIME || ',' || STATUS || ',' || ATTRIBUTE2 || ',' || ATTRIBUTE14
FROM BPMUSER.EC_PE_STATUS
WHERE START_TIME<(SYSDATE - 15) AND (STATUS='pi_halted' OR STATUS='pi_failed')
ORDER BY START_TIME DESC;
FIN_SQL

Invalid reference of variable 'v_date_extraction'

CREATE OR REPLACE PROCEDURE bcy_genera_file_cedacri_G2 (p_errbuff OUT VARCHAR2, p_errcode OUT NUMBER, p_data_lancio IN date, p_directory IN VARCHAR2) is
file_csv utl_file.file_type;
v_src_file BFILE;
v_content BLOB;
g_application varchar2(3) := 'OIC';
g_ambient varchar2(4) := 'TEST';
g_ret_code_exception_value number := 2;
g_ret_code_exception number := 0;
v_date_extraction varchar2(15); --yyyymmddhhMM ..
creazione_file_except exception;
v_p_date_launching date;
cursor cur_csv is
SELECT flag_pubblicato, data_pubblicazione
FROM XXBCYIN.bcy_supporto_garante_2 bsg2;
BEGIN
v_date_extraction:= to_char (nvl(p_date_launching, sysdate), 'yyyymmddhhMM');
BEGIN
--to create file csv
file_csv := utl_file.fopen ('p_directory', 'GARANTE2||g_application||g_ambient||v_date_extraction.csv', 'W');
FOR r IN cur_csv
LOOP
utl_file.put_line (
file_csv,
--cur_csv.ID || ';' ||
--cur_csv.TRX_TYPE || ';' ||
'USER_ID' || ';' ||
'USERNAME' || ';' ||
'DOMINIO' || ';' ||
'TIMESTAMP' || ',' ||
'CODICE_POSTAZIONE_1' || ';' ||
'CODICE_POSTAZIONE_2' || ';' ||
'CODICE_POSTAZIONE_3' || ';' ||
'CODICE_POSTAZIONE_4' || ';' ||
'CODICE_POSTAZIONE_5'|| ';' ||
'NDG_CLIENTE' || ';' ||
'CODICE_SERVIZIO' || ';' ||
'CODICE_ABI' || ';' ||
'CODICE_ISTITUTO' || ';' ||
'CODICE_CAB_OPERATORE' || ';' ||
'CODICE_OPERATORE' || ';' ||)
END LOOP;
utl_file.fclose (file_csv);
v_src_file:= 'GARANTE2'||g_application||g_ambient||v_date_extraction.csv;
At the last line I get the error :
PLS-00487 invalid reference to variable 'v_date_extraction'.
This variable is defined
v_date_extraction:= to_char (nvl(p_date_launching, sysdate), 'yyyymmddhhMM');
where v_date_extraction is a varchar2 and p_data_lancio and sysdate are date. This may be the problem?
The problem appears to be that you failed to put ".csv" in quotes. I believe the line raising the error should be:
v_src_file:= 'GARANTE2'||g_application||g_ambient||v_date_extraction||'.csv';

OBIEE Prompt and particular SQL Result

reportDimMCP and reportValMCP are two different presentation variable for two prompt. i want to represent using a string the output of a select case.
i try this :
SELECT CASE '#{reportDimMCP}{Utilizzato}' WHEN 'Numero' THEN '( case '#{reportValMCP} {NULL}' ' || when ||' ' Utilizzo > 0' '|| then || 'ho scelto Numero con Utilizzo > 0' || when ||' ' Utilizzo = 0' ' || then 'ho scelto Numero con Utilizzo = 0' || end || ')'
FROM "Myself"
but doesn't work
Any idea?
Is the problem that your second case is incorrectly inside quotes? i.e. should it look more like this:
case '{pvTest1}{A}'
when 'A' then
case '{pvTest2}{X}'
when 'X' then 'Foo'
else 'Bar'
end
else 'wibble'
end

replace only exact words with a list of words from a table

this question is a part of this one
limit records in cursor by using a variable
the original question was a 2 part question, 1 part being, IS THERE A BETTER WAY TO DO THIS
i have code that works but it's EXTREMELY slow, about 2 seconds for each record.
so i have a table, NR_POSTAL_ABBR, that has 2 fields and about 400 records
ReplaceWhat ReplaceWith
Ave Avenue
St Street
i want to be able to replace the Address field in another table with the valules from the table above
so if i have an address 123 Main St - it should say 123 Main Street
if i have 123 Main Street - it should stay 123 Main Street, it shouldn't become 123 Main Streetreet
the table with addresses has a few million rows, is there a fast way of doing this?
thank you
So as you asked. My suggestion is go through regular expressions So here is un update code for your case.
Suppose you have a table test with the field address, what you have to do is very simple, create an regular expression that match your requirements.
So: You need to replace every St for Street and Av for Avenue this is what you should do.
update test
set address =
regexp_replace(
regexp_replace(address, 'Av | Av$| Av ', ' Avenue ' ),
'St | St$| St ', ' Street ');
Here is a SQLFiddle example
Explaining the regex:
The regexp_replace replaces a string for another based on a regular expression pattern, see the docs: REGEXP_REPLACE
About the regular expressions you can see it here Regular Expressions Wiki Almost every language follow the POSIX pattern, so once you learn it you will be good.
So I've used to regexp_replace to achieve what you want because you gave two requirements. I will not write about the parameters of the function just the expressions.
At the first expression you have 'Av | Av$| Av ' which means:
PS.: I put the - just to you see the space (so ignore it). SO wont let me put it.
-Av - (with a space at end) = Find every Av with a space after it on the string
-|- (pipe sign) = Equals an or statement
- Av$- = Find all Av with a space before it and when Av is at the end of the string. $
- Av - = Find all Av with a space before and a space after
Then The function replace any of theese ocurrences with the word Avenue. Notice that I put a space before and one after to avoid something like Peer HarborAvenue
The same explanation goes to St string
If you like the regular expressions function you can see more with it here: Oracle Regular Expressions Functions
just posting the automated version based on Jorge's reply, in case someone needs it.
DECLARE
ReplOrder NUMBER;
BEGIN
ReplOrder := 1;
DECLARE
CURSOR getReplsStrng IS
SELECT replacewhat
,replacewith
FROM analyst.NR_POSTAL_ABBR
WHERE ReplaceOrder = ReplOrder;
BEGIN
FOR getInnerRec IN getReplsStrng LOOP
-- DBMS_OUTPUT.put_line('replace what ' || getInnerRec.replacewhat);
-- DBMS_OUTPUT.put_line('Replace Order ' || ReplOrder);
UPDATE NR_TMP_106 tmp
SET NewAddress = LOWER(REGEXP_REPLACE(LOWER(NewAddress)
, '$'
|| getInnerRec.replacewhat
|| ' | '
|| getInnerRec.replacewhat
|| '$| '
|| getInnerRec.replacewhat
|| ' '
,' ' || getInnerRec.replacewith || ' '))
WHERE 1 = 1
AND ( tmp.NewAddress LIKE '%' || CHR(32) || '' || getInnerRec.replacewhat || '' || NULL || '%'
OR tmp.NewAddress LIKE '%' || CHR(32) || '' || getInnerRec.replacewhat || '' || CHR(32) || '%'
OR tmp.NewAddress LIKE '%' || NULL || '' || getInnerRec.replacewhat || '' || CHR(32) || '%');
COMMIT;
END LOOP;
END;
ReplOrder := 2;
DECLARE
CURSOR getReplsStrng IS
SELECT replacewhat
,replacewith
FROM analyst.NR_POSTAL_ABBR
WHERE ReplaceOrder = ReplOrder;
BEGIN
FOR getInnerRec IN getReplsStrng LOOP
-- DBMS_OUTPUT.put_line('replace what ' || getInnerRec.replacewhat);
-- DBMS_OUTPUT.put_line('Replace Order ' || ReplOrder);
UPDATE NR_TMP_106 tmp
SET NewAddress = LOWER(REGEXP_REPLACE(LOWER(NewAddress)
, '$'
|| getInnerRec.replacewhat
|| ' | '
|| getInnerRec.replacewhat
|| '$| '
|| getInnerRec.replacewhat
|| ' '
,' ' || getInnerRec.replacewith || ' '))
WHERE 1 = 1
AND ( tmp.NewAddress LIKE '%' || CHR(32) || '' || getInnerRec.replacewhat || '' || NULL || '%'
OR tmp.NewAddress LIKE '%' || CHR(32) || '' || getInnerRec.replacewhat || '' || CHR(32) || '%'
OR tmp.NewAddress LIKE '%' || NULL || '' || getInnerRec.replacewhat || '' || CHR(32) || '%');
COMMIT;
END LOOP;
END;
END;

Resources