简体   繁体   中英

Leading and trailing spaces in CSV data exported from SQL

I am using a function to pull data from an SQL database and echo it to a file, to be subsequently e-mailed. However some of the fields in the CSV file (when opened with Microsoft Excel) are showing leading/trailing spaces.

Example of the SQL:

function pull_data
{
    sqlplus -s $user/$pass@$db <<EOF
    SET COLSEP ,
    SET LINESIZE 10000
    SET PAGESIZE 50000
    SET NEWPAGE NONE
    SET HEADING OFF
    SET FEEDBACK OFF
    SET ECHO OFF
    SELECT DISTINCT order_number,
                    TO_CHAR(entry_date,'DD/MM/YYYY'),
                    TO_CHAR(delivery_date,'DD/MM/YYYY'),
                    cust_name,
                    ...
}

echo "$(pull_data $a $b)" > $id.csv

Example of the spacing (couldn't upload through Stack Overflow):

http://s3.postimg.org/ekgt9ig4f/Capture.png

What can be done to get the output correct ie no leading or trailing spaces?

Ok, so it doesn't actually matter in this case how the columns are selected. The trimming can be done via the OS shell instead of trying to do it in Oracle.

Change the last line from:

echo "$(pull_data $a $b)" > $id.csv

to

echo "$(pull_data $a $b)" | tr -d '\040\011' > $id.csv

This uses the linux tr (translate) utility to remove spaces (\\040) and tabs (\\011) from the output.

Edit:

If the output needs to includes explicit spaces, then replace them with character(s) that are not present in the data, but this is a rather inelegant hack. Change:

SELECT COL1,
       COL2 || ' ' || COL3
FROM   ...

to

SELECT COL1,
       COL2 || '###' || COL3
FROM   ...

and the last line becomes:

echo "$(pull_data $a $b)" | tr -d '\040\011' | tr --squeeze-repeats [#] ['\040'] > $id.csv

Tim Hall wrote an article on this:

http://www.oracle-base.com/articles/9i/generating-csv-files.php

I find his site one of the best sources on Oracle stuff

If you have CHAR columns use TRIM function to remove leading/trailing spaces

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM