Export .txt from Sql issue - sql

I want to export a .txt file from a sql script. I don't want any headings. I also don't want any commas (,) dividing my fields and I want them to keep the " which surrounds each value in my table. My code runs but includes headings, removes " from each field and inserts a ,.
My code
set echo off
set verify off
set termout on
set heading off
set pages 50000
set feedback off
set newpage none
set linesize 1000
set serveroutput off
spool New_Members.txt
SELECT 'Unique_ID, Name, Alt_Name, Entity_Type, Party_Type, Reference_#, Addr1, Addr2, Addr3, Town, County, Postcode,
Country,ALT_ADDRESS1,ALT_ADDRESS2,ALT_ADDRESS3,ALT_TOWN,ALT_COUNTY,ALT_POST_CODE,ALT_COUNTRY,NATIONALITY,
DOB,INDIVIDUAL_ID,INDIVIDUAL_ID_TYPE,COUNTRY_OF_REGISTRATION,COMPANY_ID,COMPANY_ID_TYPE,SOURCE_COUNTRY,SOURCE_SYSTEM,TRANSACTION_TYPE'
from dual;
/
select Unique_ID||','||Name||','||Alt_Name||','|| Entity_Type||','||Party_Type||','||Reference_#||','||Addr1||','||Addr2||','||
Addr3||','||Town||','||County||','||Postcode||','||Country||','||ALT_ADDRESS1||','||ALT_ADDRESS2||','||
ALT_ADDRESS3||','||ALT_TOWN||','||ALT_COUNTY||','||ALT_POST_CODE||','||ALT_COUNTRY||','||NATIONALITY||','||
DOB||','||INDIVIDUAL_ID||','||INDIVIDUAL_ID_TYPE||','||COUNTRY_OF_REGISTRATION||','||COMPANY_ID||','||COMPANY_ID_TYPE||','||
SOURCE_COUNTRY||','||SOURCE_SYSTEM||','||TRANSACTION_TYPE
from dbo.Temp_Weekly_Export_File;
/
spool off;
exit

You already have set header off, so you won't see any Oracle-generated column headings. Setting pagesize to zero would also suppress the headers. You may find it helpful to also set trimout on and set trimspool on.
What you seem to be seeing is the CSV header that you are explicitly generating with your first query. If you don't want to see that header line, just remove it from your script.
If you keep that header, remove the extra whitespace and particularly the line breaks, as they will cause the header to be treated as multiple rows when import to Excel etc. If you were trying to stop that line being too long in your script, you could concatenate multiple shorter strings instead:
select 'Unique_ID,Name,Alt_Name,Entity_Type,Party_Type,Reference_#,'
|| 'Addr1,Addr2,Addr3,Town,County,Postcode,Country,ALT_ADDRESS1,'
|| 'ALT_ADDRESS2,ALT_ADDRESS3,ALT_TOWN,ALT_COUNTY,ALT_POST_CODE,'
|| 'ALT_COUNTRY,NATIONALITY,DOB,INDIVIDUAL_ID,INDIVIDUAL_ID_TYPE,'
|| 'COUNTRY_OF_REGISTRATION,COMPANY_ID,COMPANY_ID_TYPE,'
|| 'SOURCE_COUNTRY,SOURCE_SYSTEM,TRANSACTION_TYPE'
from dual;
You are running both of your queries twice, because each of them is terminated with a semicolon (;) which submits that statement; and is then followed by a slash (/), which re-executes the current command buffer. You only need one or the other, so I'd remove both the slashes - but whichever you remove, be consistent, and check if there are coding guidelines in your organisation which prefer one over the other.
As noted in comments, your question is a bit confused, as you're explicitly adding commas and there are no double-quotes to remove. You may actually want to add double-quotes though, if any of the column values can contain the comma delimiter - which would confuse Excel (or whatever will use this file). You can concatenate double-quotes around specific fields as required, e.g.:
select Unique_ID||','||Name||','||Alt_Name||','|| Entity_Type||','||
Party_Type||','||Reference_#||',"'||Addr1||'","'||Addr2||'",'||
...
which would enclose the first two address column values in double-quotes in the output.

Related

How to replace some characters after a specific character to another specific character in one big sql line in notepad++

I have a big sql file with thousand user something like this:
('someone1#mydomain.com','{SSHA512}JWHCqHzazH2vGneLPfhMKkoAamzvxdNCWYOlhZ+uDx36jHdoMXwQmbEemvUMn7ZG6c9+22noXjjb2hAb99/5A/slscDJPKav','','en_US','maildir','Maildir','/home/vmail','vmail1','mydomain.com/someone1/',0,'mydomain.com','','','normal','',0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,NULL,'1970-01-01 01:01:01',0,'',NULL,NULL,'2020-03-19 13:15:58','2015-08-03 06:11:53','2020-03-19 13:15:58','9999-12-31 00:00:00',1'someone1'),
('someone2#mydomain.com','{SSHA512}UoMeyocmdC2DxM0S7B4WFdjnCNuvkngzzLus33h9nugKVlvdhlcboKmMDDuAkCHEyLBUgf8DicKWFPJVS7EOF/ytv27MQ3Ch','','en_US','maildir','Maildir','/home/vmail','vmail1','mydomain.com/someone2/',0,'mydomain.com','','','normal','',0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,NULL,'1970-01-01 01:01:01',0,'',NULL,NULL,'2015-12-17 12:27:35','2015-08-03 06:44:10','2021-06-08 06:55:33','9999-12-31 00:00:00',1'someone2'),
('someone3#mydomain.com','{SSHA512}A6ToCf4OfP3XNEU9ngEmGN/LDquH9+s9Qxme3SoJaDyVvxiWpnwwTiAALSdnmhIxDB2VQK0zhdF+jP8ARvh0N3IDL0Xv/KmL','','en_US','maildir','Maildir','/home/vmail','vmail1','mydomain.com/someone3/',0,'mydomain.com','','','normal','',0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,NULL,'1970-01-01 01:01:01',0,'',NULL,NULL,'2018-04-03 12:31:09','2015-08-03 06:50:01','2018-04-03 12:31:18','9999-12-31 00:00:00',1'someone3'),
('someone4#mydomain.com','{SSHA512}t7/JbUPQ+rtKeRTgWRH6KlETr2JsqYORBOZouzOzs4Wo6YfHYLoy0m+U4kZXk+AeNgMep2hGZSodPZdK2l2bn9MhOKHOuF/L','','en_US','maildir','Maildir','/home/vmail','vmail1','mydomain.com/someone4/',0,'mydomain.com','','','normal',''0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,NULL,'1970-01-01 01:01:01',0,'',NULL,NULL,'2020-03-18 07:48:26','2016-11-14 06:59:04','2021-06-08 05:54:28',9999-12-31 00:00:00',1'someone4')
And now I need to delete the last word ('someone1' , 'someone2' , 'someone3' , 'someone4') for every user which adjoining to 1. It will be looks like
....9999-12-31 00:00:00',1)
not like in original
....9999-12-31 00:00:00',1'someone1')
....9999-12-31 00:00:00',1'someone2')
etc
But don't forget they are not in different lines. All this is in one big line and this makes me to ask you help. Thanks a lot.
It seems that (from your examples) the rows do not contain any parentheses except their start and end characters. So you can search for one quotation mark ', and a number of letters and/or digits, and one quotation mark ', and than ).
To do this;
Open Replace window in Notepad++ by using ctrl+h shortcut
From Search Mode section select Reqular expression
Write '[a-zA-Z0-9]*?[-,_,.]*?[a-zA-Z0-9]*?[-,_,.]*?[a-zA-Z0-9]*?[-,_,.]*?[a-zA-Z0-9]*?'\) to Find what box
Write '\) to Replace with box
Click Replace All button.
This works if user names consist of letters or digits and _, -, . at most 3 times.
Be Sure that you have a copy of original file as a backup. And also be aware of that the regular expression that we use may find unrelated parts if any row contains closing parentheses except end of it.

Is there a possibility to delete a string that starts with a ">" and ends with a newline in postgresql via sql update statement?

Lets say I have a database with about 50k entries in a column called content.
This column contains strings which causes problems to my further work.
Now here is the thing I need to do it for all the rows inside of that table.
Any Ideas?
Here an example:
'user wrote:
-----------------------------------------------------
> Some text
> that vary too much and I dont need it actually
> here is end of the text
The text I actually need.'
I would like to remove all of the unnecessary part so the only thing that is left is in this case :
'The text I actually need.'
This should delete all lines that start with a >:
regexp_replace(textcol, E'^>.*\n', '', 'gn');
The g flag is needed to delete all such lines, and the n flag makes the ^ match the position right after each line break.
I use an “extended” string literal (the leading E) so that I can write a newline as \n.

Set Escape in a View

I currently am attempting to run a script which contains a case statement where I need to match on the string 'Prod. & Mat.'
When running the script in a worksheet, I simply precede my entire script by running,
set escape '\'
Then I run my select statement which includes the case portion written as 'Prod. \& Mat.'
All matches up as expected. However, I need to generate this same script in a view. As you know the view begins with the header...
Create or replace force view "database.table" as .....
My problem, I cannot figure out how/get the view to run and acknowledge the set escape '\' piece, so my results do not match to my 'Prod. & Mat.' portion.
How or where do I insert my set escape clause, or is there another way to avoid the & problem within a view?
Try adding set define offbefore running your create view script, and then set define on afterwards, if you want ampersands to represent parameters again.
That way, & is treated as just another character - there will be no need to escape it.

Adding numbers containing commas in a batch script

I'm trying to add two numbers together in a windows batch file. The numbers are coming from the output of a command and I cannot change the code to output it in a different format.
The problem is that the numbers use commas in the numbers as the thousands separator. i.e. 154022 is output as 154,022. Now when I try to add this number to another number it only adds the first part (i.e. that 154).
set A=1,000
set B=154,022
set /a TOTAL=A + B
echo %TOTAL%
produces: 155, not 155022 that I would like, or even 155,022 would do.
Is there a way to convert easily from numbers with commas to numbers without commas in a batch script?
set A=1,000
set B=154,022
set A2=%A:,=%
set B2=%B:,=%
set /a TOTAL=A2 + B2
echo %TOTAL%
You can do string manipulation like this
set result=%input:substring=replacement%
This one and other nice tips: http://www.dostips.com/DtTipsStringManipulation.php

How do I ignore ampersands in a SQL script running from SQL Plus?

I have a SQL script that creates a package with a comment containing an ampersand (&). When I run the script from SQL Plus, I am prompted to enter a substitute value for the string starting with &. How do I disable this feature so that SQL Plus ignores the ampersand?
This may work for you:
set define off
Otherwise the ampersand needs to be at the end of a string,
'StackOverflow &' || ' you'
EDIT: I was click-happy when saving... This was referenced from a blog.
If you sometimes use substitution variables you might not want to turn define off. In these cases you could convert the ampersand from its numeric equivalent as in || Chr(38) || or append it as a single character as in || '&' ||.
I resolved with the code below:
set escape on
and put a \ beside & in the left 'value_\&_intert'
Att
You can set the special character, which is looked for upon execution of a script, to another value by means of using the SET DEFINE <1_CHARACTER>
By default, the DEFINE function itself is on, and it is set to &
It can be turned off - as mentioned already - but it can be avoided as well by means of setting it to a different value. Be very aware of what sign you set it to. In the below example, I've chose the # character, but that choice is just an example.
SQL> select '&var_ampersand #var_hash' from dual;
Enter value for var_ampersand: a value
'AVALUE#VAR_HASH'
-----------------
a value #var_hash
SQL> set define #
SQL> r
1* select '&var_ampersand #var_hash' from dual
Enter value for var_hash: another value
'&VAR_AMPERSANDANOTHERVALUE'
----------------------------
&var_ampersand another value
SQL>
set define off <- This is the best solution I found
I also tried...
set define }
I was able to insert several records containing ampersand characters '&' but I cannot use the '}' character into the text
So I decided to use "set define off" and everything works as it should.
According to this nice FAQ there are a couple solutions.
You might also be able to escape the ampersand with the backslash character \ if you can modify the comment.
I had a CASE statement with WHEN column = 'sometext & more text' THEN ....
I replaced it with
WHEN column = 'sometext ' || CHR(38) || ' more text' THEN ...
you could also use
WHEN column LIKE 'sometext _ more text' THEN ...
(_ is the wildcard for a single character)