Script to generate EXCEL sheet by shell script
Hi ,
i am generating some data by firing sql query with connecting to the database by my solaris box. I am getting data as below... TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM -------------------------------------------------------------------------------- FST_NM -------------------------------------------------------------------------------- DOJ_EMp QVD_CD PLC_CD ----------- -------- -------- DESC_TXT -------------------------------------------------------------------------------- 13-09-2014 34653436 John Sendrie TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM -------------------------------------------------------------------------------- FST_NM -------------------------------------------------------------------------------- DOJ_EMp QVD_CD PLC_CD ----------- -------- -------- DESC_TXT -------------------------------------------------------------------------------- 16-DEC-2008 SO MPCHG1 PCHG1 (Continuous PVR excess - more than 180 days) But i am expecting in the excel format like as below TO_CHAR(C.DTD_ACY_START_DTM_UTC+8/24,'DD-MM-YYYY') CURR_EMP_NO LST_NM FST_NM DOJ_EMP QV_CD PL_CD DESC_TXT 13-09-2014 34653436 John sendrie 23-APR-2007 MCH1 Hospitalised (Continous hospitalised 1 month or more and long Medical) 13-09-2014 4736543 michal thomas 16-DEC-2008 SO MPCHG1 PCHG1 (Continuous PVr excess - more than 180 days) In short each row means each column and bold one is the header of my excel sheet (containg hundreds of records), so i need data in each corresponding coulmn. whatever query iam firing by shell script result should be in EXcel format and then iam ftpying this excel to my superior. can anyone assist me on this. |
this is the right forum to get help (so someone will surely help you). But first you need to tell us your problem (to be able to assist). Do you have a script already? How does your query work?
|
You can't really make the SQL server return data in "Excel format", as that is a proprietary Microsoft file format, but I suspect you mean a list of comma-separated values, which Excel (and other spreadsheet software) is able to interpret directly.
Which SQL server are we talking about here, and what does the SQL query look like, exactly? |
Hi Ser/pan,
Thanks for reply, Yes script is ready to extract the data , here only i need data to be in a proper format may be comma separated format so i can have a file with xls and send to the superior one. The result should be the excel on whatever going at back end dosnt matter,boss should be able to read data... SQL query is look like below(query is being fully altered) Query result is perfectly correct SELECT TO_CHAR(C.PVR_ACY_START_DTM_UTC+8/24,'DD-MM-YYYY'),D.CURR_EMP_NO,D.LST_NM,D.FST_NM,D.DOJ_EMP,C.VTP_CD,A.VPA_CD,B.DESC_TXT FROM HFG_ACYS A,VMX_USER.JDS_EMP_PLX_TYPES B,EMP E,EMP1 D And C.Dtd_asdb_Start_Dtm_Utc+8/24 <= Sysdate + 1 (fdmf_ACY_END_DTM_UTC+8/24,'DD-MM-YYYY'); Problem is Data format, its in line by line format as shown below. |
Quote:
So, post your script, tell us about your environment, and we can try to help. |
Below is the script which is actually contain nothing only the query.
Operating system :SUNOS/Solaris Database : oracle Current script giving me the correct but inform of data , not inform of record/table/excel. #!/bin/ksh sqlplus -s /nolog << EOF CONNECT test/test@IP alter session set nls_date_format= 'DD-MON-YYYY'; SELECT TO_CHAR(C.PVR_ACY_START_DTM_UTC+8/24,'DD-MM-YYYY'),D.CURR_EMP_NO,D.LST_NM,D.FST_NM,D.DOJ_EMP,C.VTP_CD,A.VPA_CD,B.DESC_TXT FROM HFG_ACYS A,VMX_USER.JDS_EMP_PLX_TYPES B,EMP E,EMP1 D And C.Dtd_asdb_Start_Dtm_Utc+8/24 <= Sysdate + 1 (fdmf_ACY_END_DTM_UTC+8/24,'DD-MM-YYYY'); end; / EOF |
You should probably use the LISTAGG function; see the Oracle documentation for details.
You didn't provide the Oracle version number. LISTAGG is not available in older versions, but the documentation provides alternatives. Please heed the warnings and do NOT under any circumstances use WM_CONCAT. |
Quote:
https://community.oracle.com/thread/2222442?tstart=0 |
i do this sometimes in db2:
Quote:
|
Nope, data is getting sepaerated by comma and in same way it is displaying in excel no columnar put which is expected.
emp_no:002-00,ndsbnfbd:01-OCT-2014,ndbnbfs:01-OCT-201 4,EMP_Type:B,FD:345,PVB:PN,Duty_Number:1,Item_Number:1,COMPANY_CODE:HG,1 i |
Quote:
|
This is the exact output sample for one row getting after executing the script
-------------------------------------------------------------------------------- Code:
EMPLOYEE_Number:002-00,CTC_Date_Utc:01-OCT-2014,DTC_Date_Local:01-OCT-201 Code:
EMPLOYEE_Number CTC_Date_Utc DTC_Date_Local EMP_Type PVR Duty_Number Item_number Company Code I have added a "," as after each column as suggested in query. |
Quote:
https://community.oracle.com/thread/2389479 http://docs.oracle.com/cd/B10501_01/...a90842/ch5.htm You were already directed to the Oracle documentation...did you read it yet? And if you don't want to use Oracle to do this (which you can), you can easily strip out anything from the comma to the colon (such as ,Departure_Station:), using a simple SED statement. Again, that is overkill, since you can tell Oracle to leave off the column headers, and just return the data. |
every rdbms i used (mysql, db2, udb, sqlite, ...), the output would be column names on the top with space-padded data in the following rows.
is oracle really that wierd ? maybe something like[untested]: Code:
... | tr ',' '\n' | cut -d : -f 2 | tr '\n' ',' |
Maybe no script needed, just mysql query like SELECT ... INTO OUTFILE 'file.csv'
And with options like FIELDS TERMINATED BY ',' ... ENCLOSED BY '"' ... LINES TERMINATED BY '\n' |
All times are GMT -5. The time now is 12:57 PM. |