Formatting the output of an SQL query - sql

I am using the Solaris OS. From Solaris I'm logging into SQL*Plus.
My database is Oracle 9i.
I am spooling the output of my query into a file. How can I get it in CSV format so that I can copy it into Excel?
My query is like the follwing.
select name,id,location from employee;

I have found a way out of it.
We can use concatenation here,
select name,id,location from employee;
gives us 2 different columns, but not in CSV format.
I did
select name||','||id||','||location from employee;
We get the output in a CSV format. It has just concatenated the output with commas (,).

Related

Search for a long list of names in SQL

I have a CSV list with a few thousand names (first, last) and I would like to search for them in SQL. How can I do this easiest?
Edit: I'm using oracle obiee so I'm somewhat limited. Not sure if I can just load a csv into a temporary table since my access is limited. Will try this tomorrow at work since I just left. Using join on it sounds quite clever to my beginner ears though.
Follow these steps
Import CSV files into your SQL server of your choice.
Use the following sql statement: select * from <table name> where <last name column> = 'smith';
I ended up doing
WHERE 'first name'||'last name'||'first name' LIKE '%john%smith%'
The first name is concatenated both at beginning and end so that it mathes both 'john smith' and 'smith john'.

SQL Where clause from a file on Oracle

I have a requirement to construct an SQL that has a where clause which is expected to look into a file entries to be used in that clause.
SELECT DISTINCT '/',t_05.puid, ',', t_01.puid,'/', t_01.poriginal_file_name
FROM PWORKSPACEOBJECT t_02, PREF_LIST_0 t_03, PPOM_APPLICATION_OBJECT t_04, PDATASET t_05, PIMANFILE t_01
WHERE t_03.pvalu_0 = t_01.puid AND t_02.puid = t_03.puid AND t_03.puid = t_04.puid AND t_04.puid = t_05.puid AND t_02.puid IN ( 'izeVNXjf44e$yB',
'gWYRvN9044e$yB' );
The above is the SQL query. As you can see the IN clause has two different strings ( puids ) that are to be considered. But in my case, this list is like 50k entries long and would come from splunk and will be in a text file.
Sample output of the text file looks as belows:
'gWYRvN9044e$yB',
'DOZVpdOQ44e$yB',
'TlfVpdOQ44e$yB',
'wOWRehUc44e$yB',
'wyeRehUc44e$yB',
'w6URehUc44e$yB',
'wScRehUc44e$yB',
'yzXVNXjf44e$yB',
'guWRvN9044e$yB',
'QiYRehUc44e$yB',
'gycRvN9044e$yB'
I am not an SQL guru, but a quick google on this gave me a reference to OPENROWSET construct, which is not available on Oracle.
Can you please suggest some pointers on what can be done to circumvent the problem.
Thanks,
Pavan.
Consider using an external table, SQL Loader or perhaps loading the file into a table in the application layer and querying it normally.
I would recommend creating a Global Temporary table, adding the rows to that table, and then joining to your temp table.
How to create a temporary table in Oracle
Other options:
You could also use pipelined functions:
https://oracle-base.com/articles/misc/pipelined-table-functions
Or use the with as... construct to fold the data into the SQL. But that would create a long SQL statement.

Error in use PostGIS SQL function with two select statement

I am new to PostGIS and SQL with a lot of things I need to learn.
I tried this SQL query:
Select ST_AsText(Select line from pathway1f)
I got errors.
Select line from pathway1f
This query works fine; thousands of lines are returned.
I want to decode the query result using ST_AsText().
It seems like I can't use two select statements. What's the correct syntax?
You can use the geometry column name directly
Select ST_AsText(line)
from pathway1f;

How to get the CSV file generated from SQL store procedure can have text format?

We have a store procedure which generates the csv file from query results. Sometimes when the field gets numeric value, the 0 in the value is truncating as the generated file is in General format. I tried the below query by adding ' infront of the field with 0 so that excel will consider it as a text and the values cannot be truncated...seems doesnt work.
select Cust_No as Cust#,'''+SERIAL_NO as Serial#
from tblCustDetails
Can anybody help?
Have you tried this?
select Cust_No as Cust#,''''+SERIAL_NO as Serial# from tblCustDetails

Query to save to csv with counter

I need your assistance. I am running the below sql query from Oracle SQL Developer so the results can be save to a csv file. I run this query using the Run Script and I can see the records that are being save to the csv file.
How can I modify the query so it can show me the number of records being save to the file? For example something like printing 1000 of 200000.
spool c:\temp\myoutputfile.csv
select * from mytable;
spool off;
You can add rownum column to your query result like below
select rownum, mytable.* from mytable;
Note, SQL doesn't have other options to print some text separately while selection is under progress. Of course you have option to print some hard coded text as part of a resulted column. Otherwise you may have to write PL SQL procedure to meet your requirement.